Member-only story

Use UUID as primary key of Laravel Eloquent ORM.

Jangwook Kim
2 min readMar 19, 2020
Laravel x Eloquent ORM

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();
});

Extends Model Class

--

--

Jangwook Kim
Jangwook Kim

Written by Jangwook Kim

Korean, live in Japan. The programmer. I love to learn something new things. I’m publishing my toy projects using GitHub. Visit https://www.jangwook.net.

No responses yet