dive-be/laravel-snowflake is a Laravel package for generate identifiers using twitter snowflake.
It currently has 8 GitHub stars and 785 downloads on Packagist (latest version 1.3.0).
Install it with composer require dive-be/laravel-snowflake.
Discover more Laravel packages by dive-be
or browse all Laravel packages to compare alternatives.
Last updated

This package assists you in creating Snowflake identifiers for your Eloquent models.
It is a Laravel wrapper for godruoyi/php-snowflake.
Please refer to the original library for more information regarding Snowflakes.
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',
];
⚠️ Use a high-performing cache driver such as
Redisto ensure rapid ID generation.
❗️ Do not use an ephemeral cache driver such as
arrayin production!
snowflake to define a Snowflake columnforeignSnowflake to reference another Snowflake (alias for foreignId)Schema::table('products', static function (Blueprint $table) {
$table->snowflake();
$table->foreignSnowflake('variant_id')->constrained();
});
Use the HasSnowflake trait in your Eloquent models:
class Product extends Model
{
use HasSnowflake;
}
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) {}
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.
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.