This package simply overrides the default grammar file for the given connection making the uuid()
blueprint method return a binary(16)
rather than the default char(36)
.
MySQL is the only supported connection type, only because I've no experience with other drivers. I welcome any pull requests to implement this functionality for other database drivers.
Note that doctrine/dbal
does not appear to support changing existing uuid
fields, and doing so would cause your existing values to be truncated in any event.
For more information, check out this post on storing and working with UUID in an optimised manner.
Using UUIDs in Laravel is made super simple in combination with laravel-model-uuid. Note that when using laravel-model-uuid
, if you are not casting your UUIDs or calling the query builder directly, you'll need to use the getBytes
method when setting the UUID on the database, otherwise your values will be truncated. Depending on your MySQL/MariaDB configuration, this may lead to application errors due to strict settings. See #1 for more information.
Laravel | Package |
---|---|
5.4.* | 1.0.* |
5.5.* | 2.0.* |
5.6.* | 2.1.* |
5.7.* | 2.2.* |
This package is installed via Composer. To install, run the following command.
composer require dyrynda/laravel-efficient-uuid
Register the service provider in your config/app.php
configuration file:
'providers' => [
...
Dyrynda\Database\LaravelEfficientUuidServiceProvider::class,
],
There is nothing special needed for this to function, simply declare a uuid
column type in your migration files. I indexing the UUID column if you plan on querying against it, but would avoid making it the primary key.
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->uuid('uuid')->index();
$table->string('title');
$table->text('body');
$table->timestamps();
});
You will need to add a cast to your model when using laravel-model-uuid in order to correctly set and retrieve UUID from your MySQL database with binary fields.
<?php
namespace App;
use Dyrynda\Database\Support\GeneratesUuid;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use GeneratesUuid;
protected $casts = ['uuid' => 'uuid'];
}
If you are having general issues with this package, feel free to contact me on Twitter.
If you believe you have found an issue, please report it using the GitHub issue tracker, or better yet, fork the repository and submit a pull request.
If you're using this package, I'd love to hear your thoughts. Thanks!