UUID LARAVEL
WHY USE UUID?
UUID (Universally Unique Identifier) is a 128-bit number used to uniquely identify information in computer systems. It is a 36-character string that is unique across all space and time. UUID is a better alternative to auto-incrementing integers because it is more secure and less predictable.
Steps:
-
create migration and model, for example using table name 'Products'
php artisan make:model Product -m -
open the migration file and change the data type of the id field to uuid
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; return new class extends Migration { public function up() { Schema::create('products', function (Blueprint $table) { $table->uuid('id')->primary(); // change the data type to uuid $table->string('name'); $table->timestamps(); }); } public function down() { Schema::dropIfExists('products'); } }; -
run the migration
php artisan migrate -
open the model file and add the following code
namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Concerns\HasUuids; class Product extends Model { use HasFactory, HasUuids; protected $guarded = ['id']; // optional, if using fillable, remove this line and change fillable to protected $fillable = ['name']; } -
testing using tinkers on terminal
php artisan tinkerApp\Models\Product::create(['name' => 'Product 1']); -
check the data in the database, the id field should be filled with a UUID