| Package Data | |
|---|---|
| Maintainer Username: | kra8 |
| Maintainer Contact: | koki@asai.email (Koki Asai) |
| Package Create Date: | 2017-10-14 |
| Package Last Update: | 2025-05-06 |
| Home Page: | |
| Language: | PHP |
| License: | MIT |
| Last Refreshed: | 2025-10-26 03:18:16 |
| Package Statistics | |
|---|---|
| Total Downloads: | 334,631 |
| Monthly Downloads: | 11,580 |
| Daily Downloads: | 50 |
| Total Stars: | 177 |
| Total Watchers: | 1 |
| Total Forks: | 22 |
| Total Open Issues: | 3 |
This Laravel package to generate 64 bit identifier like the snowflake within Twitter.
composer require "kra8/laravel-snowflake"
php artisan vendor:publish --provider="Kra8\Snowflake\Providers\LaravelServiceProvider"
composer require "kra8/laravel-snowflake"
// Add this line
$app->register(Kra8\Snowflake\Providers\LumenServiceProvider::class);
Get instance
$snowflake = $this->app->make('Kra8\Snowflake\Snowflake');
or
$snowflake = app('Kra8\Snowflake\Snowflake');
Generate snowflake identifier
$id = $snowflake->next();
Add the Kra8\Snowflake\HasSnowflakePrimary trait to your Eloquent model.
This trait make type snowflake of primary key. Don't forget to set the Auto increment property to false.
<?php
namespace App;
use Kra8\Snowflake\HasSnowflakePrimary;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use HasSnowflakePrimary, Notifiable;
/**
* Indicates if the IDs are auto-incrementing.
*
* @var bool
*/
public $incrementing = false;
}
Finally, in migrations, set the primary key to bigInteger, unsigned and primary.
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
// $table->increments('id');
$table->bigInteger('id')->unsigned()->primary();
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
If config/snowflake.php not exist, run below:
php artisan vendor:publish