ufucms / laravel-snowflake
分布式唯一ID,基于雪花算法。
1.0.3
2023-08-10 03:51 UTC
Requires
- php: ^8.0|^8.1
- illuminate/support: ^6.3|^7.0|^8.0|^9.0|^10.0
This package is auto-updated.
Last update: 2024-12-20 15:22:57 UTC
README
这个 Laravel 包生成 64 位标识符,就像 Twitter 中的雪花一样。
Laravel 安装
composer require "ufucms/laravel-snowflake"
php artisan vendor:publish --provider="Ufucms\Snowflake\Providers\LaravelServiceProvider"
Lumen 安装
- Install via composer
composer require "ufucms/laravel-snowflake"
- Bootstrap file changes 将以下代码段添加到 providers 部分下的 bootstrap/app.php 文件中,如下所示:
// Add this line $app->register(Ufucms\Snowflake\Providers\LumenServiceProvider::class);
Usage
Get instance
use Ufucms\Snowflake\Snowflake; $snowflake = new Snowflake();
or
$snowflake = $this->app->make('Ufucms\Snowflake\Snowflake');
or
$snowflake = app('Ufucms\Snowflake\Snowflake');
Generate snowflake identifier
$id = $snowflake->nextId();
or
$id = $snowflake->id();
or
$id = Snowflake::nextId();
Usage with Eloquent
将 Ufucms\Snowflake\HasSnowflakePrimary
特征添加到您的 Eloquent 模型中。
此特征使主键类型为snowflake
。 Trait 会自动将 $incrementing 属性设置为 false。
<?php namespace App; use Ufucms\Snowflake\HasSnowflakePrimary; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use HasSnowflakePrimary, Notifiable; }
Column type id
is supported.
/** * Run the migrations. * * @return void */ public function up() { Schema::create('users', function (Blueprint $table) { $table->id(); $table->string('name'); $table->string('email')->unique(); $table->string('password'); $table->rememberToken(); $table->timestamps(); }); }
JavaScript support
由于 JavaScript 无法处理 64 位整数,因此还有 HasShortPrimary,它为 JavaScript 可以处理的 53 位整数创建 ID。
<?php namespace App; use Ufucms\Snowflake\HasShortPrimary; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use HasShortPrimary, Notifiable; }
Configuration
If config/snowflake.php
not exist, run below:
php artisan vendor:publish