-
Notifications
You must be signed in to change notification settings - Fork 11.5k
[9.x] UUID and ULID support for Eloquent #44074
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8f15950
Eloquen UUID & ULID
driesvints 78fb93b
wip
driesvints c22597a
wip
driesvints 4e37b71
Update tests/Integration/Database/EloquentPrimaryUlidTest.php
driesvints 08d0c03
wip
driesvints 0c4bcbb
wip
driesvints 1866016
wip
driesvints be6df86
wip
driesvints 1cf1148
tweaking
taylorotwell 003b1a3
update composer file
taylorotwell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
namespace Illuminate\Database\Eloquent\Concerns; | ||
|
||
use Illuminate\Support\Str; | ||
|
||
trait HasUlids | ||
{ | ||
/** | ||
* Boot the trait. | ||
* | ||
* @return void | ||
*/ | ||
public static function bootHasUlids() | ||
{ | ||
static::creating(function (self $model) { | ||
foreach ($model->uniqueIds() as $column) { | ||
if (empty($model->{$column})) { | ||
$model->{$column} = $model->newUniqueId(); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Generate a new ULID for the model. | ||
* | ||
* @return string | ||
*/ | ||
public function newUniqueId() | ||
{ | ||
return strtolower((string) Str::ulid()); | ||
} | ||
|
||
/** | ||
* Get the columns that should receive a unique identifier. | ||
* | ||
* @return array | ||
*/ | ||
public function uniqueIds() | ||
{ | ||
return [$this->getKeyName()]; | ||
} | ||
|
||
/** | ||
* Get the auto-incrementing key type. | ||
* | ||
* @return string | ||
*/ | ||
public function getKeyType() | ||
{ | ||
return 'string'; | ||
} | ||
|
||
/** | ||
* Get the value indicating whether the IDs are incrementing. | ||
* | ||
* @return bool | ||
*/ | ||
public function getIncrementing() | ||
{ | ||
return false; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
namespace Illuminate\Database\Eloquent\Concerns; | ||
|
||
use Illuminate\Support\Str; | ||
|
||
trait HasUuids | ||
{ | ||
/** | ||
* Generate a primary UUID for the model. | ||
* | ||
* @return void | ||
*/ | ||
public static function bootHasUuids() | ||
{ | ||
static::creating(function (self $model) { | ||
foreach ($model->uniqueIds() as $column) { | ||
if (empty($model->{$column})) { | ||
$model->{$column} = $model->newUniqueId(); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Generate a new UUID for the model. | ||
* | ||
* @return string | ||
*/ | ||
public function newUniqueId() | ||
{ | ||
return (string) Str::orderedUuid(); | ||
} | ||
|
||
/** | ||
* Get the columns that should receive a unique identifier. | ||
* | ||
* @return array | ||
*/ | ||
public function uniqueIds() | ||
{ | ||
return [$this->getKeyName()]; | ||
} | ||
|
||
/** | ||
* Get the auto-incrementing key type. | ||
* | ||
* @return string | ||
*/ | ||
public function getKeyType() | ||
{ | ||
return 'string'; | ||
} | ||
|
||
/** | ||
* Get the value indicating whether the IDs are incrementing. | ||
* | ||
* @return bool | ||
*/ | ||
public function getIncrementing() | ||
{ | ||
return false; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace Illuminate\Tests\Integration\Database; | ||
|
||
use Illuminate\Database\Eloquent\Concerns\HasUlids; | ||
use Illuminate\Database\Eloquent\Model as Eloquent; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
use Illuminate\Support\Str; | ||
|
||
class EloquentUlidPrimaryKeyTest extends DatabaseTestCase | ||
{ | ||
protected function defineDatabaseMigrationsAfterDatabaseRefreshed() | ||
{ | ||
Schema::create('users', function (Blueprint $table) { | ||
$table->char('id', 26)->primary(); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
public function testUserWithUlidPrimaryKeyCanBeCreated() | ||
{ | ||
$user = UserWithUlidPrimaryKey::create(); | ||
|
||
$this->assertTrue(Str::isUlid($user->id)); | ||
} | ||
} | ||
|
||
class UserWithUlidPrimaryKey extends Eloquent | ||
{ | ||
use HasUlids; | ||
|
||
protected $table = 'users'; | ||
|
||
protected $guarded = []; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace Illuminate\Tests\Integration\Database; | ||
|
||
use Illuminate\Database\Eloquent\Concerns\HasUuids; | ||
use Illuminate\Database\Eloquent\Model as Eloquent; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
use Illuminate\Support\Str; | ||
|
||
class EloquentUuidPrimaryKeyTest extends DatabaseTestCase | ||
{ | ||
protected function defineDatabaseMigrationsAfterDatabaseRefreshed() | ||
{ | ||
Schema::create('users', function (Blueprint $table) { | ||
$table->uuid('id')->primary(); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
public function testUserWithUuidPrimaryKeyCanBeCreated() | ||
{ | ||
$user = UserWithUuidPrimaryKey::create(); | ||
|
||
$this->assertTrue(Str::isUuid($user->id)); | ||
} | ||
} | ||
|
||
class UserWithUuidPrimaryKey extends Eloquent | ||
{ | ||
use HasUuids; | ||
|
||
protected $table = 'users'; | ||
|
||
protected $guarded = []; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there something wrong with the requirement?
I had to manually
composer require symfony/uid
to getStr::ulid()
to work.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is in the
suggests
session. So to use its functionality you should manually install this dependency.