Installation:
composer require simlux/laravel-model-uuid:dev-master
Add the service provider to config/app.php (if not auto-discovered):
Simlux\LaravelModelUuid\LaravelModelUuidServiceProvider::class,
First Use Case:
UuidModelTrait to a model:
use Simlux\LaravelModelUuid\Uuid\UuidModelTrait;
class MyModel extends Model
{
use UuidModelTrait;
}
UuidMigrationHelper to add the uuid column and unique index:
use Simlux\LaravelModelUuid\Migration\UuidMigrationHelper;
Schema::create('my_models', function (Blueprint $table) {
$table->unsignedBigInteger('id', true); // Auto-incrementing ID
UuidMigrationHelper::uuid($table); // Adds UUID column + unique index
});
First Query:
// Create a record with auto-generated UUID
$model = MyModel::create(['name' => 'Test']);
// Find by UUID
$model = MyModel::uuid($model->uuid)->first();
// Find by UUID (alternative syntax)
$model = MyModel::where('uuid', $model->uuid)->first();
Model Setup:
UuidModelTrait in all models requiring UUIDs. No additional configuration is needed beyond the trait and migration helper.class User extends Model
{
use UuidModelTrait;
protected $fillable = ['name', 'email'];
}
Migrations:
UuidMigrationHelper::uuid($table) in your up() method for new tables.$table->string('uuid')->unique();
and update the model to use the trait.Querying:
uuid() scope for cleaner queries:
// Get a model by UUID
$user = User::uuid($request->uuid)->firstOrFail();
// Get multiple models by UUIDs
$users = User::whereIn('uuid', $uuidArray)->get();
Seeding:
Str::uuid() to generate UUIDs in seeders:
User::create([
'uuid' => Str::uuid(),
'name' => 'Admin',
]);
APIs and Serialization:
uuid).{
"id": 1,
"uuid": "a1b2c3d4-5678-90ef-ghij-klmnopqrstuv",
"name": "Test"
}
Relationships:
class Post extends Model
{
use UuidModelTrait;
public function author()
{
return $this->belongsTo(User::class, 'author_uuid', 'uuid');
}
}
Testing:
$uuid = '00000000-1111-2222-3333-444444444444';
$model = MyModel::uuid($uuid)->create(['name' => 'Test']);
Auto-Incrementing ID vs. UUID:
id column (as shown in the migration example). If you only want UUIDs, remove the id column and update the model to use public $incrementing = false;.class MyModel extends Model
{
use UuidModelTrait;
public $incrementing = false;
protected $keyType = 'string';
protected $primaryKey = 'uuid';
}
Unique Index Conflicts:
uuid column without the unique index, the package will not automatically add it. Always use UuidMigrationHelper::uuid($table) for consistency.Schema::table('my_models', function (Blueprint $table) {
$table->string('uuid')->unique()->change();
});
Case Sensitivity:
// Use strtolower/strtoupper if needed
$model = MyModel::where('uuid', strtolower($uuid))->first();
Hybrid Primary Keys:
id to uuid as the primary key, update your model:
protected $primaryKey = 'uuid';
public $incrementing = false;
protected $keyType = 'string';
php artisan cache:clear and php artisan config:clear after changes.Soft Deletes:
SoftDeletes, ensure the UUID is included in queries:
$model = MyModel::withTrashed()->uuid($uuid)->first();
Missing UUID Column:
uuid() queries fail, verify the column exists in the database and is named uuid.UUID Generation Issues:
uuid column is not set to nullable.UuidModelTrait is properly used in the model.boot() method is overriding the UUID logic.Performance:
Custom UUID Generation:
boot() method to your model:
protected static function boot()
{
parent::boot();
static::creating(function ($model) {
$model->uuid = Str::orderedUuid(); // Use a custom UUID generator
});
}
UUID Formatting:
getUuidAttribute or setUuidAttribute methods:
public function getUuidAttribute($value)
{
return strtoupper($value); // Store/retrieve UUIDs in uppercase
}
Custom Migration Helper:
UuidMigrationHelper to add options (e.g., length, index name):
UuidMigrationHelper::uuid($table, [
'length' => 36,
'indexName' => 'custom_uuid_index',
]);
UUID as Foreign Key:
trait UuidForeignKey
{
public function setForeignKey($key)
{
$this->foreignKey = $key . '_uuid';
}
}
Usage:
class Post extends Model
{
use UuidForeignKey;
public function author()
{
return $this->belongsTo(User::class);
}
}
How can I help you explore Laravel packages today?