Repository navigation
[9.x] UUID and ULID support for Eloquent - #44074
Conversation
|
@driesvints Awesome PR - I was just thinking about using ULIDs in my next project. Yesterday Symfony added UUID v7 (which is similar to ULID) support (symfony/symfony#47525) to |
|
|
I'm not sure. We don't use |
Done |
| public static function bootHasUlidPrimaryKey() | ||
| { | ||
| static::creating(function (self $model) { | ||
| if (empty($model->{$model->getKeyName()})) { |
There was a problem hiding this comment.
Shouldn't be blank($model->getKey()) ?
|
Just came here to say that ULID will be saved into the database as a 26 character string, which occupies 208 bits, almost double than an UUID (128 bit). If the column is an UUID, most database engines will return an error. What it could work is setting the key type to "uuid", so then the primary key is detected as an ULID, it will change to RFC4122. |
It seems like |
|
I'll look into it 👍 |
|
Maybe creating a Currently I'm adding the following macro to the AppServiceProvicer: Blueprint::macro('ulid', function (string $column = 'ulid') {
return $this->addColumn('char', $column, ['length' => 26]);
});Which enables me to do this in my migrations: Schema::create('users', function (Blueprint $table) {
$table->ulid('id')->primary();
...
}); |
|
Awesome! Just this week i'd created a trait that used |
|
@larskoole I'll also check into that. |
|
@driesvints Could we have an option to set which column to be the uuid? For example, we could keep the default behaviour of looking at the primary key but it would be nice to have something like Potential usage: table: |
Already there, you can override the |
|
If someone wanted to use this for columns other than the primary |
I'll think about that a bit more as well. |
|
Worked on some of the above stuff here: #44146. I'll check into UUID v7 support later. |
| "league/commonmark": "Required to use Str::markdown() and Stringable::markdown() (^2.0.2).", | ||
| "ramsey/uuid": "Required to use Str::uuid() (^4.2.2).", | ||
| "symfony/process": "Required to use the composer class (^6.0).", | ||
| "symfony/uid": "Required to generate ULIDs for Eloquent (^6.0).", |
There was a problem hiding this comment.
Is there something wrong with the requirement?
I had to manually composer require symfony/uid to get Str::ulid() to work.
There was a problem hiding this comment.
It is in the suggests session. So to use its functionality you should manually install this dependency.
|
I sent in a PR to use uuid v7 in Laravel v10 here: #44210 |
|
@driesvints @taylorotwell I added missing morphs methods for ULID support 👉 PR 44364 😊 |
|
We reverted the changes to make UUID v7 the default in Laravel v10. Please see #44210 |
This PR brings UUID & ULID support for Eloquent as primary keys. By simply applying a trait, any Eloquent model will have a UUID or ULID generated as their primary key.
For ULID support, an optional
symfony/uiddependency will need to be pulled in.