kra8 / laravel-snowflake
Snowflake for Laravel and Lumen.
Fund package maintenance!
kra8
Installs: 374 941
Dependents: 6
Suggesters: 0
Security: 0
Stars: 181
Watchers: 1
Forks: 22
Open Issues: 5
pkg:composer/kra8/laravel-snowflake
Requires
- php: ^8.2
- illuminate/support: ^6.3|^7.0|^8.0|^9.0|^10.0|^11.0|^12.0
Requires (Dev)
- orchestra/testbench: ^8.0
- phpunit/phpunit: ^10.0|^11.0|^12.0
- dev-main
- v2.4.1
- v2.4.0
- v2.3.0
- v2.2.0
- v2.1.2
- v2.1.1
- v2.1.0
- v2.0.4
- v2.0.3
- v2.0.2
- v2.0.1
- v2.0.0
- v1.3.2
- v1.3.1
- v1.3.0
- v1.2.3
- v1.2.2
- v1.2.1
- v1.2.0
- v1.1.0
- v1.0.0
- dev-dependabot/composer/symfony/process-6.4.33
- dev-dependabot/composer/phpunit/phpunit-10.5.62
- dev-dependabot/composer/symfony/http-foundation-6.4.29
This package is auto-updated.
Last update: 2026-01-28 21:47:32 UTC
README
This Laravel package to generate 64 bit identifier like the snowflake within Twitter.
Laravel Installation
composer require "kra8/laravel-snowflake"
php artisan vendor:publish --provider="Kra8\Snowflake\Providers\LaravelServiceProvider"
Lumen Installation
- Install via composer
composer require "kra8/laravel-snowflake"
- Bootstrap file changes Add the following snippet to the bootstrap/app.php file under the providers section as follows:
// Add this line $app->register(Kra8\Snowflake\Providers\LumenServiceProvider::class);
Usage
Get instance
$snowflake = $this->app->make('Kra8\Snowflake\Snowflake');
or
$snowflake = app('Kra8\Snowflake\Snowflake');
Generate snowflake identifier
$id = $snowflake->next();
Usage with Eloquent
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.
<?php namespace App; use Kra8\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
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; }
Configuration
If config/snowflake.php not exist, run below:
php artisan vendor:publish