Wallogit.com
2017 © Pedro Peláez
Easily use UUIDs as primary keys
Simplifies using UUIDs in MySQL and Laravel, (*1)
https://noolan.github.io/my-uuid/, (*2)
composer require noolan/my-uuid
php artisan vendor:publish --provider Noolan\\MyUuid\\Service
The settings for this package can now be edited in config/myuuid.php., (*3)
| Key | Type | Default | Description |
|---|---|---|---|
mysql_8 |
Boolean | true |
MySQL 8 adds two new functions, UUID_TO_BIN and BIN_TO_UUID that simplify working with UUIDs.If mysql_8 is true, those new functions will be used instead of the mashup of HEX, UNHEX, and REPLACE that is otherwise required.If you are unsure which version of MySQL you have you can run the php artisan myuuid:version command detailed below. |
connection |
String |
'' (empty string) |
The name of the database connection to use as defined in your config/database.php file.An empty string will result in your default connection being used. |
There are two Artisan commands included with this package that help with configuration; version and check., (*4)
| Command | Arguments | Description | Example |
|---|---|---|---|
myuuid:version |
(none) | Outputs the MySQL version of the configured connection. | php artisan myuuid:version |
myuuid:check |
(none) | Checks the current configuration against the database to see if there are issues. | php artisan myuuid:check |
All the functionality is accessed through the MyUuid facade., (*5)
use MyUuid;
/* ... */
public function doAThing()
{
$myUuid = MyUuid::alter('examples');
/* ... */
}
Most methods on the MyUuid class return the object so methods can be chained., (*6)
$myUuid->addColumn('uuid', 'blob', 16)
$myUuid->addColumn('parent_id', 'varbinary', 36)
$myUuid->addIndex();
$myUuid->run();
// Can be re-written as:
$myUuid->addColumn('uuid', 'blob', 16)
->addColumn('parent_id', 'varbinary', 36)->addIndex()
->run();
Note: If a non-column function is called without a column name parameter, MyUuid uses the last added column as a default., (*7)
There are several functions that make it easy to perform common tasks as long as you don't need to deviate from the default parameters., (*8)
/* Add an auto-populating, 16 byte, binary column named 'id'
and use it as the table's primary key */
$myUuid->addPrimaryUuid('id');
// is equivalent to:
$myUuid->addColumn('id', 'binary', 16)
->addIndex('primary')
->addTrigger();
Columns and indexes created with MyUuid can be dropped with Laravel's Schema builder. The only thing you have to manually drop is triggers., (*9)
// removes auto-population trigger attached to the 'id' column
$myUuid->dropTrigger('id')->run();
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use MyUuid;
class CreateExamplesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('examples', function (Blueprint $table) {
$table->timestamps();
$table->softDeletes();
$table->string('title')->index();
$table->text('description');
$table->longText('code');
});
MyUuid::alter('examples')
->addPrimaryUuid('id')->withFriendly('uuid')
->addForeignUuid('category_id')
->run();
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
MyUuid::alter('examples')->dropTrigger('id')->run();
Schema::dropIfExists('characters');
}
}