Implement UUID on Authentication Built-in Laravel 5.7

Didin Ahmadi
Dec 9, 2018 · 3 min read
Implement UUID on Laravel 5.7

Hi guys,

I want to implement UUID on my Laravel 5.7 project. By default Laravel has provided great features in it, we can have authentication by just running simple command php artisan make:auth, then Laravel will provide these all for us. We can have a login’s page, a register’s page and a forgot password’s page just in second.

But I’m not trying to explain this now, and I supposed you have already know this or you can check this out here .

Now when you have Laravel’s built-in authentication feature that by default it was implemented auto-increment’s integer on its user ID.

So how if we want to change it to UUID based? Here it is I’m going to share with you all about this.

Create migration to change `id` column to accept UUID

Run this command:

php artisan make:migration change_user_id_to_uuid —table=users

This command above will generate migration file like this:

2018_12_08_134048_change_user_id_to_uuid.php

Then open up migration file that just created, you may see different file name because migration will create based on timestamp you run at.
And make sure you modify that migration’s file like just code below.

<?phpuse Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class ChangeUserIdToUuid extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->string('id', 36)->change();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('users', function (Blueprint $table) {
$table->integer('id')->change();
});
}
}

Then run migrate to start changing your database: php artisan:migrate . If you found some problems regarding migration, try to truncate your users’s table first, and make sure users’s table empty before running migration again.

Modify User’s Eloquent Model

After you run migration and succeeded, then you can modify User’s model, open up app\User.php

I’m using UUID Generator library from webpaster, so if you want to follow me, you should add

composer require “webpatser/laravel-uuid”: “3.0”

then run command composer update then do the following changes like code below:

<?phpnamespace App;use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Webpatser\Uuid\Uuid;class User extends Authenticatable
{
use Notifiable;
// set to false, so we tell that primary-key field
// not auto incrementing anymore
public $incrementing = false;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
public static function boot()
{
parent::boot();
self::creating(function($model){
$model->id = self::generateUuid();
});
}
public static function generateUuid()
{
return Uuid::generate();
}
}

I also made some UUID Wrapper for Laravel to generate UUID, you can use it too if you want and here it is

Anyway, hope this help and see you later …

Didin Ahmadi

Written by

Fulltime Laravel Developer

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