Table of contents
- What are UUID
- Create new Laravel project
- Configure Database Credentials
- Uuid Trait
- Preparing User Model and Migration
- Install Laravel Breeze
- Testing our Trait
- GitHub Repo
1. What are UUID.
UUID stands for Universally Unique IDentifier. It's a 128-bit value used for a unique identification in software development.
If you want to learn more about UUID you can check this article What are UUIDs, and should you use them?
2. Create new Laravel Project
In your terminal execute
composer create-project laravel/laravel uuid-project
Note: Since, Laravel 5.6, ramsey/uuid library is part of Laravel, so you don't have to install it by yourself to generate the UUID.
3. Configure Database Credentials
Open your .env, put your credentials
4. Uuid Trait
In this case, I'm going to create a trait because it's more flexible and we can use it in every model we want.
Let's create a new folder inside app called Trait and file UuidTrait.php like this
Open the new file and use the following code
<?php
namespace App\Traits;
use Illuminate\Support\Str;
trait UuidTrait
{
/**
* Boot function from Laravel.
*/
protected static function boot()
{
parent::boot();
static::creating(function ($model) {
if (empty($model->{$model->getKeyName()})) {
$model->{$model->getKeyName()} = Str::uuid()->toString();
}
});
}
/**
* Get the value indicating whether the IDs are incrementing.
*
* @return bool
*/
public function getIncrementing()
{
return false;
}
/**
* Get the auto-incrementing key type.
*
* @return string
*/
public function getKeyType()
{
return 'string';
}
}
In this case, we need to specify that the primary key is string using getKeyType
We need to disable auto-incrementing id using getIncrementing() returning false
The boot method will generate the UUID.
Don't forget to import Illuminate\Support\Str;
5. Preparing User Model and Migration
Let's go to database/migrations search for users migration file, change the default id:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
You can see we change $table->id() for $table->uuid('id')->primary() our migration file is ready, now we can go to app/Models/User.php, add the trait
<?php
namespace App\Models;
use App\Traits\UuidTrait;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable, UuidTrait;
/**
* The attributes that are mass assignable.
*
* @var string[]
*/
protected $fillable = [
'name',
'email',
'password',
];
}
After Notifiable Trait we need to add UuidTrait, our model is ready!!!
Execute in your terminal
php artisan migrate
Let's test if this works by making an authentication system with Laravel Breeze.
6. Install Laravel Breeze
To install Laravel Breeze execute this command in your terminal
composer require laravel/breeze
After installing the package, run this command
php artisan breeze:install
To finish this part you need to run
npm install && npm run dev
Note: You need to have installed node.js to execute npm install && npm run dev
7. Testing our Trait
Let's register a new user
This is the moment we are waiting, we need to verify in our database if we store and UUID instead of a regular ID.
This works. How cool is that!!!
8. GitHub Repo
If you have any doubt you can check the repo Laravel-UUID or leave a comment down below.
Happy coding โ. Thanks for reading ๐.