cloudcake/laravel-uuid

UUID's for Laravel's Eloquent models.

v1.5 2020-04-08 08:26 UTC

This package is auto-updated.

Last update: 2024-03-27 05:00:16 UTC


README

Since Laravel 9.x, UUID support for Eloquent is baked right into Laravel.
See Illuminate/Database/Eloquent/Concerns/HasUuids.

This package is now redundant.

Installation

Install via composer using composer require cloudcake/laravel-uuid

Usage

Primary Key UUID's

If you want to use UUID's as your model's primary key, this is for you.

  • Add the Uuid\Traits\UuidPrimaryKey trait to the eloquent model
  • Add the primary key column name if you're not using the default of id (Optional)
use Uuid\Traits\UuidPrimaryKey;

class User extends Authenticatable
{
    use UuidPrimaryKey;

    protected $primaryKey = 'id';
}

Now you'll be able to call User::find('<uuid-here>');.

Additional Field UUID's

In some situations you may want to retain your regular integer based primary key, but add an additional UUID column to your models, for this case, use the Uuid trait.

  • Add the Uuid\Traits\Uuid trait to the eloquent model
  • Add the name of the UUID column
use Uuid\Traits\Uuid;

class User extends Authenticatable
{
    use Uuid;

    protected static $uuidKeyName = 'uuid';
}

And query it as you would any other attribute, User::where('uuid', '<uuid-here>')->first().

Additional Options

Add any/all of the following to your model(s) to fine-tune the package to your needs.

Attribute Description
$uuidKey The name of the UUID column in the database. Defaults to uuid.
$uuidOrdered Whether or not the UUID should use timestamp ordering. Defaults to true.

Example:

use Uuid\Traits\Uuid;
use Illuminate\Database\Eloquent\Model;

class Comment extends Model
{
    use Uuid;

    /**
     * The name of the UUID column in the database.
     *
     * @var string
     */
    protected static $uuidKey = 'universally_unique_id';

    /**
     * Whether or not the UUID should use timestamp ordering.
     *
     * @var bool
     */
    protected static $uuidOrdered = true;
}