Skip to content
Auto create and save uuid on eloquent models.
PHP
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
tests
.gitattributes
.gitignore
.php_cs.dist
.phpunit-watcher.yml
.scrutinizer.yml
.styleci.yml
.travis.yml
CHANGELOG.md
CONTRIBUTING.md
LICENSE.md
README.md
composer.json
phpunit.xml.dist

README.md

Laravel Auto Create Uuid on Eloquent Models

Build Status StyleCI Quality Score Latest Stable Version Total Downloads 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 info@mindtwo.de instead of using the issue tracker.

Credits

License

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

You can’t perform that action at this time.