Skip to content

mindtwo/laravel-auto-create-uuid

Repository files navigation

Laravel Auto Create Uuid on Eloquent Models

Build Status StyleCI Quality Score Latest Stable Version Total Downloads MIT Software License

Installation

You can install the package via composer:

composer require mindtwo/laravel-auto-create-uuid

How to use?

Prepare eloquent model

Simply use the AutoCreateUuid trait in your eloquent model.

namespace example;

use Illuminate\Database\Eloquent\Model;
use mindtwo\LaravelAutoCreateUuid\AutoCreateUuid;

class Example extends Model
{
    use AutoCreateUuid;
}

Add UUID column to migration

Make sure to add the column in your migration file.

$table->string('uuid', 36)->unique();

Customize UUID attribute

The default attribute name for the auto generated UUID is 'uuid'. However you can customize it, if you need. There are two possibilities to do so.

Either you set up a property named 'uuid_column':

namespace example;

use Illuminate\Database\Eloquent\Model;
use mindtwo\LaravelAutoCreateUuid\AutoCreateUuid;

class Example extends Model
{
    use AutoCreateUuid;
    
    protected $uuid_column = 'id'
}

or you overload the getUuidColumn() method:

namespace example;

use Illuminate\Database\Eloquent\Model;
use mindtwo\LaravelAutoCreateUuid\AutoCreateUuid;

class Example extends Model
{
    use AutoCreateUuid;
    
    /**
     * Get the column name for uuid attribute.
     *
     * @return string
     */
    public function getUuidColumn(): string
    {
        return 'id';
    }
}

In both cases the attribute name for the UUID is now 'id' instead of 'uuid'.

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.