1) Implement Uuid trait.
<?php
namespace App\Traits;
use Exception;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
trait Uuid
{
protected static function boot()
{
parent::boot();
static::creating(function (Model $model) {
try {
$model->id = Str::uuid()->toString();
} catch (Exception $e) {
abort(500, $e->getMessage());
}
});
}
}
2) Go to the model class and set $incrementing to false;
class Whatever implements Model {
public $incrementing = false;
...
}
3) Go to the migration class and set uuid as primary key;
class CreateWhateverTable extends Migration {
public function up()
{
Schema::create("whatever", function (Blueprint $table) {
$table->uuid("id")->primary();
});
}
}