How to use UUIDs instead of auto-increment IDs in your Laravel app

116c34de-3dcb-44bc-9511-a48242b9aaab
composer create-project --prefer-dist laravel/laravel laravel-uuid-demo
composer create-project --prefer-dist laravel/laravel your-project-name "7.0.*"
Image by author. Source: https://laravel.com/docs/5.6/releases#laravel-5.6
composer require ramsey/uuid

Let’s get coding

namespace App\Models;
<?phpnamespace App\Traits;use Illuminate\Support\Str;trait Uuids
{
/**
* 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';
}
}
<?phpnamespace App\Traits;use Ramsey\Uuid\Uuid;trait Uuids
{
/**
* Boot function from Laravel.
*/
protected static function boot()
{
parent::boot();
static::creating(function ($model) {
if (empty($model->{$model->getKeyName()})) {
$model->{$model->getKeyName()} = Uuid::uuid4()->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';
}
}
$table->id();
$table->uuid('id')->primary();
php artisan migrate

Testing

use App\User;
use App\Models\User;
php artisan make:seeder UserSeeder
factory(User::class, 6)->create();
php artisan db:seed
(NB: remember_token, created_at, updated_at columns not shown)

Bonus Tip:

$table->bigInteger('user_id')->unsigned();
$table->uuid('user_id');
(NB: the profile table primary key [id] does not need to be a UUID type.)

--

--

--

Software developer. On twitter @MarieInnov

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Spring @Cachable not working

Creating AWS Elastic Container Service for Kubernetes (EKS) the Right Way

Object Oriented Programming (OOP) — Pt. 1

SchemaUI — Turn your mongoose schema into a fascinating Admin Dashboard

AndroidX Navigation with Dagger 2 & FragmentFactory

Change As A Norm: Adapting To Andela’s Bootcamp

Let’s use Git(Version Control) to suit each occasion.😉 (part-1)

Why The Future of Custom Software Development Won’t Be Custom

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Marie Mbenoun

Marie Mbenoun

Software developer. On twitter @MarieInnov

More from Medium

How to Setup Cloud Firestore and PHP for Development

Integrate PHP application with Firebase

Detailed Guide on Laravel Development Proces

Stripe ACH implementation in laravel