You don't want to use auto incrementing ID in your model?
Migration:
1Schema::create('users', function (Blueprint $table) {2 // $table->increments('id');3 $table->uuid('id')->unique();4});
Laravel 9 and above:
1use Illuminate\Database\Eloquent\Concerns\HasUuids; 2use Illuminate\Database\Eloquent\Model; 3 4class Article extends Model 5{ 6 use HasUuids; 7 8 // ... 9}10 11$article = Article::create(['title' => 'Traveling to Europe']);12 13$article->id; // "8f8e8478-9035-4d23-b9a7-62f4d2612ce5"
Laravel 8 and below:
Model:
- In PHP 7.4.0 and above:
1use Illuminate\Support\Str; 2use Illuminate\Database\Eloquent\Model; 3 4class User extends Model 5{ 6 public $incrementing = false; 7 protected $keyType = 'string'; 8 9 protected static function boot()10 {11 parent::boot();12 13 self::creating(fn (User $model) => $model->attributes['id'] = Str::uuid());14 self::saving(fn (User $model) => $model->attributes['id'] = Str::uuid());15 }16}
- In PHP older than 7.4.0:
1use Illuminate\Support\Str; 2use Illuminate\Database\Eloquent\Model; 3 4class User extends Model 5{ 6 public $incrementing = false; 7 protected $keyType = 'string'; 8 9 protected static function boot()10 {11 parent::boot();12 13 self::creating(function ($model) {14 $model->attributes['id'] = Str::uuid();15 });16 self::saving(function ($model) {17 $model->attributes['id'] = Str::uuid();18 });19 }20}