Setup UUID as Primary Key in Laravel 7
Tags: uuid, laravel, laravel7, laravelmigrations
Published: 2021-02-01 14:58:54

---
title: Setup UUID as Primary Key in Laravel 7
published: true
date: 2020-05-25 15:28:15 UTC
tags: uuid,laravel,laravel7,laravelmigrations
canonical_url: https://medium.com/@bervianto.leo/setup-uuid-as-primary-key-in-laravel-7-4be5cfe9f650
---

In case you never heard about Laravel, you can visit [here](https://laravel.com).

![](https://cdn-images-1.medium.com/max/1024/0*P-Q9hsfvTj5vo7Ah)<figcaption>Photo by <a href="https://unsplash.com/@cgower?utm_source=medium&amp;utm_medium=referral">Christopher Gower</a> on <a href="https://unsplash.com?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption>

Default primary key for Laravel Model is using increment integer, for [reference](https://laravel.com/docs/7.x/eloquent#eloquent-model-conventions). In some case, developer want to use another type such as UUID. What is UUID? In short, **UUID** stands for **Universally Unique IDentifier** , for more explanation, please read also [here](https://tools.ietf.org/html/rfc4122).

You only need some steps to change your Model so use UUID as default.

1. Create Uuid Trait, for example I place the file in **App\Traits** with name **Uuid.php**. In this example, we use id column as our primary key, you can use another column as your want.

_Notes: In case you want to know what is trait, read _[_here_](https://www.php.net/manual/en/language.oop5.traits.php)_._

```
<?php

namespace App\Traits;

use Illuminate\Support\Str;

trait Uuid
{
    protected static function boot()
    {
        parent::_boot_();

        static::_creating_(function ($model) {
            try {
 **$model->id = (string) Str::_uuid_(); // generate uuid**  
                // Change id with your primary key
            } catch (UnsatisfiedDependencyException $e) {
                abort(500, $e->getMessage());
            }
        });
    }
}
```

2. Change your [Model](https://laravel.com/docs/7.x/eloquent#defining-models), for example we change User class. Here the change

```
<?php

namespace App;

use App\Traits\Uuid;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends _Authenticatable_
{
  **Use Uuid;**  
    use Notifiable;

    _/\*\*  
 \* Indicates if the IDs are auto-incrementing.  
 \*  
 \*_ **_@var_** _bool  
 \*/_ **public $incrementing = false;**  
  
    _/\*\*  
 \* The "type" of the auto-incrementing ID.  
 \*  
 \*_ **_@var_** _string  
 \*/_ **protected $keyType = 'uuid';**

    // ...
}
```

3. Change your [migrations](https://laravel.com/docs/7.x/migrations#introduction)files, because we use users, here the example changes.

```
_/\*\*  
 \* Run the migrations.  
 \*  
 \*_ **_@return_** _void  
 \*/_ public function up()
{
    Schema::_create_('users', function (Blueprint $table) {
 **$table->uuid('id')->primary(); // define your primary key column**  
        // another column
    });

}

 **$table->uuid('id')->primary();**  
        // another column
    });

}
```

Hopefully this will help you.

For another Models that use same column, you can reuse the Uuid trait and do some change same with step 2 and 3.