这个 Laravel 包可以生成类似 Twitter 内部雪花的 64 位标识符。
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. Trait will automatically set $incrementing property to false.
id();
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}Since JavaScript cannot handle 64-bit integers, there is also HasShortPrimary, which creates an ID for a 53-bit integer that can be handled by JavaScript.
To use it, simply change HasSnowflakePrimary to HasShortPrimary.
<?php
namespace App;
use Kra8\Snowflake\HasShortflakePrimary;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use HasShortflakePrimary, Notifiable;
}If config/snowflake.php not exist, run below:
php artisan vendor:publish暂无开放 Issues,或尚未同步最近议题。