frictionlessdigital / uuidable
Frictionless Solutions | Uuidable
Installs: 194
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 3
Forks: 0
Open Issues: 0
Type:project
Requires
- php: ^8.1
- illuminate/support: ^10.0|^11.0
Requires (Dev)
- fakerphp/faker: ^1.9.1
- orchestra/testbench: ^9.0
- phpunit/phpunit: ^10.0|^11.0
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.