Anggaplah kita memulai sebuah proyek baru. Kita akan mengubah ID dari tabel users menjadi sebuah UUID. Secara bawaan file migrationnya seperti ini.
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateUsersTable extends Migration
{
/**
- Run the migrations.
*
- @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
/**
- Reverse the migrations.
*
- @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
File migration di atas akan membuat sebuah ID dengan auto increment big integer. Kemudian kita bisa mengubahnya menjadi seperti di bawha ini.
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateUsersTable extends Migration
{
/**
- Run the migrations.
*
- @return void
*/
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();
});
}
/**
- Reverse the migrations.
*
- @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
Setelah itu jalankan migrationnya. Setelah tabel dibuat sekarang kita harus menonaktifkan auto increment melalui model User.
/**
- 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';
}
Kemudian untuk membuat UUID secara otomatis ketika menambahkan data. Kita akan gunakan Model Event creating. Kemudian untuk membuat UUID kita gunakan helper Str. Kurang lebih seperti ini kodenya.
<?php
namespace App;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Str;
class User extends Authenticatable
{
use Notifiable;
/**
The "booting" function of model
*
@return void
*/
protected static function boot() {
static::creating(function ($model) {
if ( ! $model->getKey()) {
$model->{$model->getKeyName()} = (string) Str::uuid();
}
});
}
/**
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';
}
/**
- 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',
];
/**
- The attributes that should be cast to native types.
*
- @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
}
Sebagai catatan ketika menggunakan UUID dalam type data string tambahkan type datanya kedalam model pada variabel $casts