Frictionless Solutions | Uuidable

1.4 2024-10-17 22:27 UTC

This package is auto-updated.

Last update: 2024-12-17 22:54:36 UTC


README

Add UUID behavior to your Eloquent models.

Installation

composer require "frictionlessdigital/uuidable":"^1.0"

Note that root namespace for the package is Fls not Frictionlessditial.

Config

The package does not require configuration.

Integration

To add behavior to your model, simply import the trait:

use Fls\Uuidable\Uuidable;

class User extends Model
{
    use Uuidable;
    ...
}

If you want to update the field to within which the UUID is stored, define a static model propery like so:

use Fls\Uuidable\Uuidable;

class User extends Model
{
    use Uuidable;
    ...

    /**
     * Name of the field to to store the UUID
     *
     * @var string
     */
    const UUID = 'diuu';
}

Usage

Fls\Uuidable\Uuidable trait adds a number of methods to your model:

Uuidable::findByUuid

Similar to find($key), findByUuid($uuid) will locate the first Uuidable model where the value of the fild matches the argument.

$user  = User::findByUuid(string $uuid);

You can also pass an array of column you want to hydrate from the database as a second argument:

$user  = User::findByUuid(string $uuid, ['email', 'password']);

Under the hood the method relies on whereUuid()

Uuidable::whereUuid()

Scope the model by uuid value. You can use a string value, an array, pass in Arrayable class. N.B. if you pass an Eloquent Collection, it will be parsed for its model keys, which will be passed as a scoping argument.

$builder  = User::whereUuid(string $uuid);

or

$builder  = User::whereUuid([$uuid]);

or

$builder  = User::whereUuid(collect([$uuid]));

or

$builder  = User::whereUuid(Users::pluck('uuid'));

Change log

Please see CHANGELOG for more information on what has changed recently.

Testing

$ composer test

Contributing

Please see CONTRIBUTING and CODE_OF_CONDUCT for details.

Security

If you discover any security related issues, please email support@frictionlesssolutions.com instead of using the issue tracker.

Credits

License

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