PHP code example of seffeng / laravel-snowflake

1. Go to this page and download the library: Download seffeng/laravel-snowflake library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

seffeng / laravel-snowflake example snippets


# 1、生成配置文件
$ php artisan vendor:publish --tag="snowflake"

# 2、修改配置文件 /config/snowflake.php,或 .env

# 1、复制扩展内配置文件 /config/snowflake.php 到项目配置目录 /config

# 2、修改配置文件 /config/snowflake.php,或 .env

# 3、将以下代码段添加到 /bootstrap/app.php 文件中的 Providers 部分
$app->register(Seffeng\LaravelSnowflake\SnowflakeServiceProvider::class);

# 4、/bootstrap/app.php 添加配置加载代码
$app->configure('snowflake');

# 示例
use Seffeng\LaravelSnowflake\Facades\Snowflake;

class SiteController extends Controller
{
    public function test()
    {
        // 生成雪花ID
        var_dump(Snowflake::id());
    }
}

# Model 示例
use Illuminate\Database\Eloquent\Model;
use Seffeng\LaravelSnowflake\Traits\SnowflakePrimary;

class User extends Model {
    // $model->save() 时自动使用雪花ID
    use SnowflakePrimary;
}