kduma / eloquent-snowflakeable
Eases using and generating SnowFlake ID's in Laravel Eloquent models.
dev-master
2023-12-29 16:29 UTC
Requires
- php: >=8.1
- godruoyi/php-snowflake: dev-snowflake-forid
This package is auto-updated.
Last update: 2024-11-29 19:02:18 UTC
README
Eases using and generating SnowFlake ID's in Laravel Eloquent models.
Setup
Install it using composer
composer require kduma/eloquent-snowflakeable
Prepare models
Inside your model (not on top of file) add following lines:
use \KDuma\Eloquent\Snowflakeable;
In database create ulid
string field. If you use migrations, you can use following snippet:
$table->unsignedBigInteger('sfid')->unique();
Usage
By default, it generates snowflake id on first save.
$model->regenerateSnowflake()
- Generate new snowflake id. (Remember to save it by yourself)Model::whereSnowflake($id)->first()
- Find by snowflake id. (whereSnowflake
is query scope)Model::bySnowflake($id)
- Find by snowflake id.$model->snowflake
- GetsParsedSnowflake
object.$model->snowflake->getDateTime()
- GetsCarbon
object with snowflake creation time.
Packagist
View this package on Packagist.org: kduma/eloquent-snowflakeable