Package Data | |
---|---|
Maintainer Username: | rorecek |
Maintainer Contact: | info@laravelist.com (Pavel Rorecek) |
Package Create Date: | 2017-08-27 |
Package Last Update: | 2024-03-02 |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-12-15 15:07:40 |
Package Statistics | |
---|---|
Total Downloads: | 128,912 |
Monthly Downloads: | 5,216 |
Daily Downloads: | 49 |
Total Stars: | 52 |
Total Watchers: | 2 |
Total Forks: | 15 |
Total Open Issues: | 6 |
Laravel package to generate ULID (Universally Unique Lexicographically Sortable Identifier), which also contains trait for your models that will let you generate ULID ids for your Eloquent models automatically. Based on robinvdvleuten/php-ulid.
In many cases universally unique identifier (UUID) can be suboptimal for many uses-cases because:
Instead, ULID offers:
You can read more here
With distributed systems you can be pretty confident that the primary key’s will never collide.
When building a large scale application when an auto increment primary key is not ideal.
It makes replication trivial (as opposed to int’s, which makes it REALLY hard)
Safe enough doesn’t show the user that you are getting information by id, for example https://example.com/item/10
You can install this package via composer using this command:
composer require rorecek/laravel-ulid:^2.0.0
There is nothing else to do as the service provider and facade are going to be automaticaly discovered.
You must install the service provider and facade:
// config/app.php
'providers' => [
...
Rorecek\Ulid\UlidServiceProvider::class,
];
...
'aliases' => [
...
'Ulid' => Rorecek\Ulid\Facades\Ulid::class,
];
When using the migration you should change $table->increments('id') to:
$table->char('id', 26)->primary();
Simply, the schema seems something like this.
Schema::create('items', function (Blueprint $table) {
$table->char('id', 26)->primary();
....
....
$table->timestamps();
});
If the related model is using an ULID, the column type should reflect that also.
Schema::create('items', function (Blueprint $table) {
$table->char('id', 26)->primary();
....
// related model that uses ULID
$table->char('category_id', 26);
$table->foreign('category_id')->references('id')->on('categories');
....
$table->timestamps();
});
To set up a model to use ULID, simply use the HasUlid trait and set the incrementing flag to false.
use Illuminate\Database\Eloquent\Model;
use Rorecek\Ulid\HasUlid;
class Item extends Model
{
use HasUlid;
....
/**
* Indicates if the IDs are auto-incrementing.
*
* @var bool
*/
public $incrementing = false;
....
}
When you create a new instance of a model which uses ULIDs, this package will automatically add ULID as id of the model.
// 'HasUlid' trait will automatically generate and assign id field.
$item = Item::create(['name' => 'Awesome item']);
echo $item->id;
// 01brh9q9amqp7mt7xqqb6b5k58
If you believe you have found an issue, please report it using the GitHub issue tracker, or better yet, fork the repository and submit a pull request.
If you're using this package, I'd love to hear your thoughts. Thanks!
The MIT License (MIT). Pavel Rorecek