Eloquent UUID-able

Eases using and generating guid's in Laravel Eloquent models.

Setup

Install it using composer

composer require kduma/eloquent-uuidable

Prepare models

Inside your model (not on top of file) add following lines:

use \KDuma\Eloquent\Uuidable;

In database create uuid string field. If you use migrations, you can use following snippet:

$table->uuid('uuid')->unique();

Usage

By default it generates slug on first save.

  • $model->regenerateUuid() - Generate new uuid. (Remember to save it by yourself)
  • Model::whereUuid($uuid)->first() - Find by guid. (whereUuid is query scope)

Upgrade from 1.x/2.x version of kduma/eloquent-guidable

Add following line to yours models to switch from using uuid column name to guid as it was used in previous versions:

protected $uuid_field = 'guid';