qh-8/laravel-snowflake

Generate Snowflake Identifier for Laravel

v1.0.0 2024-09-16 08:13 UTC

This package is auto-updated.

Last update: 2024-09-16 08:22:56 UTC


README

Latest Version on Packagist Total Downloads GitHub Actions

A Laravel package to generate unique snowflake ids.

Forked from kra8/laravel-snowflake.

Requirements

  • Laravel 10 or higher

Installation

composer require qh-8/laravel-snowflake

Usage

Using the Snowflake class to generate and decode snowflake ids.

use Qh\LaravelSnowflake\Snowflake;

$snowflake = app(Snowflake::class)->generate(); // or via Facade: Snowflake::generate()

// 93977444276639021

$data = app(Snowflake::class)->decode(93977444276639021); // or via Facade: Snowflake::decode(int $id)

//[
//  'binary_length' => 57,
//  'binary' => '101001101110111111111110011010111000000100001010100101101',
//  'binary_timestamp' => '10100110111011111111111001101011100',
//  'binary_sequence' => '010100101101',
//  'binary_worker_id' => '00001',
//  'binary_datacenter_id' => '00001',
//  'timestamp' => 22405968732,
//  'sequence' => 1325,
//  'worker_id' => 1,
//  'datacenter_id' => 1,
//  'epoch' => 1704067200000,
//  'datetime' => '2024-09-16T07:52:48+00:00',
//]

Using in the eloquent model:

use \Qh\LaravelSnowflake\HasSnowflakeIds;

class User extends Model
{
    use HasSnowflakeIds;
    
    //
}

Please note that the id column in the table should be a BIGINT type.

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Security

If you discover any security related issues, please email dqh@dinhquochan.com instead of using the issue tracker.

Credits

License

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