stayallive/laravel-eloquent-uuid
Installation:
composer require stayallive/laravel-eloquent-uuid
Add the service provider to config/app.php (if not auto-discovered):
'providers' => [
// ...
Stayallive\Laravel\Eloquent\UUID\ServiceProvider::class,
],
First Use Case:
Apply the UsesUUID trait to any Eloquent model:
use Stayallive\Laravel\Eloquent\UUID\UsesUUID;
class User extends Model
{
use UsesUUID;
}
Now, every new record will auto-generate a UUID for the primary key (id by default).
UsesUUID trait for customization options (e.g., overriding the UUID column).uuid type in PostgreSQL or char(36) in MySQL).HasUUID. Compare features if upgrading.Model Definition:
class Post extends Model
{
use UsesUUID;
protected $keyType = 'string'; // Required for UUIDs (default in Laravel 8+)
}
create().Database Schema: Ensure your migration uses a UUID-compatible column:
Schema::create('posts', function (Blueprint $table) {
$table->uuid('id')->primary(); // PostgreSQL
// OR
$table->char('id', 36)->primary(); // MySQL
});
Querying:
$post = Post::where('id', 'a1b2c3d4-...')->first(); // Works as expected
Custom UUID Column:
Override the default id column:
use Stayallive\Laravel\Eloquent\UUID\UsesUUID;
class Order extends Model
{
use UsesUUID;
protected $primaryKey = 'order_id';
}
Manual UUID Generation: Force a UUID for an existing model:
$order = new Order();
$order->forceFillUuid(); // Manually set UUID
$order->save();
Integration with APIs: Use UUIDs in API responses (Laravel’s default JSON casting handles strings):
return Post::find('a1b2c3d4-...'); // Returns UUID as string
Seeding: UUIDs work with Laravel’s seeder:
Post::factory()->create(); // Auto-generates UUID
HasUuid::fake() (Laravel 10+) or mock the trait’s generateUuid() method for predictable UUIDs in tests.SoftDeletes trait:
use Illuminate\Database\Eloquent\SoftDeletes;
use Stayallive\Laravel\Eloquent\UUID\UsesUUID;
class User extends Model
{
use UsesUUID, SoftDeletes;
}
belongsTo, hasMany).Database Compatibility:
char(36) or binary(16) for UUIDs (not varchar without a fixed length).char(36) or text (but avoid uuid extension unless enabled).SQLSTATE[22007]: Invalid datetime format → Ensure your column type matches the UUID format.Primary Key Conflicts:
$user = new User();
$user->id = 'a1b2c3d4-...'; // Must be unique!
$user->save();
Laravel 10+ Deprecation:
HasUUID instead:
use Illuminate\Database\Eloquent\Concerns\HasUuid;
class User extends Model
{
use HasUuid;
}
UUID Not Generated:
$keyType = 'string' is set.Performance:
ULID (via Laravel’s HasUlid) for better performance if needed.Logging: Enable query logging to debug UUID-related SQL errors:
DB::enableQueryLog();
$user = User::create(['name' => 'Test']);
dd(DB::getQueryLog());
Custom UUID Generation:
Override the generateUuid() method in your model:
protected function generateUuid()
{
return Str::orderedUuid(); // Laravel 10+ alternative
}
Event Hooks:
Listen for creating events to modify UUID behavior:
protected static function bootUsesUUID()
{
static::creating(function ($model) {
if (!$model->{$model->getKeyName()}) {
$model->{$model->getKeyName()} = Str::uuid()->toString();
}
});
}
Hybrid Keys:
Combine UUIDs with other traits (e.g., HasFactory, Observables) without conflicts.
protected $casts = [
'id' => 'string',
];
How can I help you explore Laravel packages today?