承接 dive-be/laravel-snowflake 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

dive-be/laravel-snowflake

Composer 安装命令:

composer require dive-be/laravel-snowflake

包简介

Generate identifiers using Twitter Snowflake

README 文档

README

Social Card of Laravel Dry Requests

❄️ Generate IDs using Twitter Snowflake

Latest Version on Packagist

This package assists you in creating Snowflake identifiers for your Eloquent models.

It is a Laravel wrapper for godruoyi/php-snowflake.

What problem does this package solve?

Please refer to the original library for more information regarding Snowflakes.

Installation

You can install the package via composer:

composer require dive-be/laravel-snowflake

You can publish the config file with:

php artisan vendor:publish --provider="Dive\Snowflake\ServiceProvider" --tag="config"

This is the contents of the published config file:

return [

    /**
     * Set this value to today when starting a new app.
     * You will have 69 years before you run out of snowflakes.
     */
    'start_date' => '2022-04-10',
];

Usage

⚠️ Use a high-performing cache driver such as Redis to ensure rapid ID generation.

❗️ Do not use an ephemeral cache driver such as array in production!

Migrations

  • Use snowflake to define a Snowflake column
  • Use foreignSnowflake to reference another Snowflake (alias for foreignId)
Schema::table('products', static function (Blueprint $table) {
    $table->snowflake();
    $table->foreignSnowflake('variant_id')->constrained();
});

Models

Use the HasSnowflake trait in your Eloquent models:

class Product extends Model
{
    use HasSnowflake;
}

Manual generation

You have a couple of options if you'd like to generate your Snowflake identifiers manually:

Snowflake::id(); // Facade
snowflake(); // Helper
app('snowflake'); // Service Locator

// Dependency Injection
public function __construct(\Godruoyi\Snowflake\Snowflake $snowflake) {}

📣 Note on JavaScript compatibility

While JavaScript itself actually supports BigInts, the JSON standard does not.

JSON.parse("{\"a\":10n}")
// Uncaught SyntaxError: Unexpected token n in JSON at position 7

Therefore, to make sure the identifiers are not truncated while deserializing them on the front-end using JSON.parse and alike, the package will automatically cast the models' id field to string.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email oss@dive.be instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

统计信息

  • 总下载量: 784
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 8
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 8
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-04-10

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固