~/docs
Resonate
Resonate
9.x 9.0.0, current 8.x 8.0.0 7.x 7.0.1 6.x 6.2.1 5.x 5.1.2 4.x 4.0.2
Laravel UUID
Toggle sidebar
Laravel UUID
webpatser/laravel-uuid 9.0.0

Laravel UUID 9.x

Eloquent Models

Use UUID primary keys on Eloquent models with the HasUuids trait and the UuidCast attribute cast.

The HasUuids trait

Webpatser\LaravelUuid\HasUuids replaces Laravel's own HasUuids trait. It builds on Laravel's HasUniqueStringIds concern, so the key is generated on creation and route model binding validates the value.

use Illuminate\Database\Eloquent\Model;
use Webpatser\LaravelUuid\HasUuids;
 
class User extends Model
{
use HasUuids;
 
protected $fillable = ['name', 'email'];
}
 
$user = User::create(['name' => 'John', 'email' => '[email protected]']);
echo $user->id; // time-ordered UUID (v7)

The migration uses Laravel's uuid column type:

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
 
return new class extends Migration
{
public function up(): void
{
Schema::create('users', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->string('name');
$table->string('email')->unique();
$table->uuid('parent_id')->nullable();
$table->timestamps();
 
$table->foreign('parent_id')->references('id')->on('users');
});
}
};

Try a round trip

The demo below defines a model with HasUuids, creates a posts table like the one above in a throwaway in-memory SQLite database, saves a row, and reads it back. The stored key is the v7 UUID the trait generated.

Title

Runs on demand, at most 10 times a minute per visitor. Nothing touches this site's database.

Choosing the UUID version

The trait generates v7 by default. Override newUniqueId() to change it:

use Webpatser\Uuid\Uuid;
 
class User extends Model
{
use HasUuids;
 
public function newUniqueId(): string
{
return (string) Uuid::v4();
}
}

The trait also exposes helpers for explicit versions and for reading the key:

Method Returns
newUniqueId() v7 string (default key generator)
newRandomUuid() v4 string
newOrderedUuid() v7 string
newTimeBasedUuid() v1 string (v1 exposes a MAC address)
getUuidVersion() version of the model key, or null when the key is empty or invalid
usesOrderedUuids() true when the key is v7
getUuidTimestamp() timestamp for v1, v6 and v7 keys, otherwise null. Not meaningful for v6 keys generated under 8.x or earlier
$user = User::create(['name' => 'John', 'email' => '[email protected]']);
 
$user->getUuidVersion(); // 7
$user->usesOrderedUuids(); // true
$user->getUuidTimestamp();

The UuidCast

Webpatser\LaravelUuid\UuidCast turns a string UUID column into a Webpatser\Uuid\Uuid object. Strings, Uuid objects and null are accepted when setting; the value is stored as the string form.

use Webpatser\LaravelUuid\UuidCast;
use Webpatser\Uuid\Uuid;
 
class User extends Model
{
use HasUuids;
 
protected $casts = [
'id' => UuidCast::class,
'parent_id' => UuidCast::class,
];
}
 
$user = User::find('550e8400-e29b-41d4-a716-446655440000');
 
$user->id->string; // "550e8400-e29b-41d4-a716-446655440000"
$user->id->hex; // "550e8400e29b41d4a716446655440000"
$user->id->version; // 4
 
$user->parent_id = Uuid::v7(); // stored as a string
$user->save();

Relationships

UUID keys work with the usual relationships.

use Illuminate\Database\Eloquent\Relations\Pivot;
 
class User extends Model
{
use HasUuids;
 
public function posts()
{
return $this->hasMany(Post::class);
}
 
public function roles()
{
return $this->belongsToMany(Role::class)
->using(UserRole::class)
->withTimestamps();
}
}
 
class Post extends Model
{
use HasUuids;
 
public function user()
{
return $this->belongsTo(User::class);
}
}
 
class UserRole extends Pivot
{
use HasUuids;
}
Schema::create('posts', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->uuid('user_id');
$table->string('title');
$table->timestamps();
 
$table->foreign('user_id')->references('id')->on('users');
});

Route model binding

Route::get('/user/{user}', [UserController::class, 'show']);
// /user/550e8400-e29b-41d4-a716-446655440000

To bind on a different column, use Laravel's own mechanisms:

Route::get('/user/{user:uuid}', [UserController::class, 'show']);

Next steps