Member-only story
Use UUID as primary key of Laravel Eloquent ORM.
2 min readMar 19, 2020
Introduction
Traditionally we use an integer value as id of model, but nowadays we use a string value as id of model called UUID(Universally Unique ID).
Eloquent ORM also has support using UUID as identifier.
In this post, I’ll teach you how to use UUID as identifier into Eloquent ORM.
Install Laravel Eloquent UUID Package
I’m using Laravel 7.x, so I entered command as shown below.
$ composer require goldspecdigital/laravel-eloquent-uuid:^7.0
Modify Migration File
Usually we use $table->id();
statement to create id column, but to use UUID, we use $table->uuid('id')->primary();
statement.
Schema::create('people', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->string('name');
$table->integer('age');
$table->timestamps();
});