Use UUID as primary key of Laravel Eloquent ORM.

Jangwook Kim
Mar 19 · 2 min read
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.


Modify Migration File

Usually we use statement to create id column, but to use UUID, we use statement.


Extends Model Class

Our field is no longer integer type. And it’s not incremented automatically. So we need to extend our model class.

It’s all. Is it too much easy, right?


Test

You can test using tinker.

One Person who has UUID as identifier was created!

Jangwook Kim

Written by

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.

More From Medium

Also tagged Uuid

Also tagged Eloquent Orm

Top on Medium

Ed Yong
Mar 25 · 22 min read

19K

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade