mykemeynell / laraveluuid
There is no license information available for the latest version (1.0.3) of this package.
UUID column functionality for Laravel projects
1.0.3
2020-07-23 18:06 UTC
Requires
- php: >=7.1.0
- ramsey/uuid: ^4.0
This package is auto-updated.
Last update: 2024-12-24 03:40:12 UTC
README
Easily start using UUID within your Laravel application, rathern than the default auto incrementing ID.
Install with Composer
composer require mykemeynell/laraveluuid
Usage
- Add a UUID column into your migration, for example the default "create users" migration.
use ...; class CreateUsersTable extends Migration { // Include the UuidColumn trait into your migration class. // This will give you access to the necessary method to easily // create the appropriate column. use \UuidColumn\Concern\UuidColumn; public function up() { Schema::create('users', function(Blueprint $table) { // Calling the createUuidColumn() method will add the column into your migration. // You can migrate this normally with PHP Artisan. $this->createUuidColumn($table, 'id); ...
- Add the
HasUUidObserver
trait to your Model. For example, on the default User model atapp/User.php
.
use ...; use UuidColumn\Concern\HasUuidObserver; class User extends Authenticatable { use ..., HasUuidObserver; ...
- You'll need to change the defaults in the model to reflect the new key type and behaviour:
class User extends Authenticatable { ... /** * Indicates if the IDs are auto-incrementing. * * @var bool */ public $incrementing = false; /** * The "type" of the primary key ID. * * @var string */ protected $keyType = 'string'; ...
That's it! When new records are created, a new UUID4 string will be set at the ID.