Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codyphobe/218e7a2c4983db81520d820e8d5c67f6 to your computer and use it in GitHub Desktop.
Save codyphobe/218e7a2c4983db81520d820e8d5c67f6 to your computer and use it in GitHub Desktop.
Using UUID with Laravel Eloquent model

Using UUID with Laravel Eloquent model

Inspirations

Background Info

Required Package

Implementation

In this example, we will use Laravel UUID package to create UUID version 4 and store it as BINARY(16) in MySQL database. The Accessor and Mutator will be used in an Eloquent model named Post to streamline the usage.

Install Laravel UUID package

This can be done via composer

composer require "webpatser/laravel-uuid=1.*"

Laravel Schema do not support BINARY(16)

The binary type is mapped to BLOB type of MySQL, so we will have to use DB::statement() to create a column of type BINARY(16) inside the migration instead.

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddUuidColumnToPostsTable extends Migration {

	public function up()
	{
		DB::statement('ALTER TABLE posts ADD uuid BINARY(16) NOT NULL AFTER id;');
		DB::statement('CREATE UNIQUE INDEX posts_uuid_unique ON posts (uuid);');
	}

	public function down()
	{
		DB::statement('DROP INDEX posts_uuid_unique ON posts;');
		DB::statement('ALTER TABLE posts DROP uuid;');
	}

}

Note that a migration for table posts should be created normally without uuid column. The above migration will modify the table to add uuid instead.

Create the model

Create the Post model manually or via artisan command, then add the Accessor and Mutator for UUID column.

<?php

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    protected $guarded = ['id];
    
    public function setUuidAttribute($value)
    {
        $this->attributes['uuid'] = hex2bin(str_replace('-', '', $value));
    }

    public function getUuidAttribute($value)
    {
        return bin2hex($value);
    }
}

Beware of Using UUID with Query Builder

When querying the table that contains BINARY(16) of UUID, there might be a problem converting BINARY(16) value to string. In that case, use DB::raw('HEX(posts.uuid) as uuid') to first convert the BINARY(16) to string.

$query = DB::table('posts')
	->select('posts.id', DB::raw('HEX(posts.uuid) as uuid'), 'posts.date', 'posts.body')
	->get();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment