lioneagle / le-utils
This is my package LeUtils
Installs: 7 803
Dependents: 1
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: ^8.1
- barryvdh/laravel-ide-helper: ^2.10
- laravel/framework: ^10.0
Requires (Dev)
- brianium/paratest: ^6.3
- nunomaduro/collision: ^6.0
- orchestra/testbench: ^7.0
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2024-04-28 11:57:16 UTC
README
Installation
Composer
$ composer require lioneagle/le-utils
UUIDs
This package provides utilities to add support for a uuid
column for Eloquent models
Usage
In your model, ensure it implements Lioneagle\LeUtils\Contracts\Uuidable
.
Use the Lioneagle\LeUtils\Traits\HasUuid
to satisfy this interface by default.
<?php namespace Lioneagle\LeUtils\Tests\Models; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\HasMany; use Lioneagle\LeUtils\Contracts\Uuidable; use Lioneagle\LeUtils\Traits\HasUuid; class User extends Model implements Uuidable { use HasUuid; protected $fillable = ['name']; public function posts(): HasMany { return $this->hasMany(Post::class); } }
Now you can query the model statically, or as part of a query;
$uuid = '942e28b5-63e3-4475-8d96-4f04ab0f627f'; $user = User::uuid($uuid); $user = User::query()->uuid($uuid);
You can also query relationships with the uuid()
method;
$post = User::posts()->uuid($uuid);
Custom Builder
By default, the HasUuid
trait will create a custom builder that will be return for new queries. The builder returned must implement Lioneagle\LeUtils\Contracts\UuidBuilderInterface
.
You can return your own builder, but it must implement the same interface. You can use the Lioneagle\LeUtils\Traits\BuilderUuidScope
trait to implement the required interface method.
<?php namespace Lioneagle\LeUtils\Query; use Illuminate\Database\Eloquent\Builder as EloquentBuilder; use Lioneagle\LeUtils\Contracts\UuidBuilderInterface; use Lioneagle\LeUtils\Traits\BuilderUuidScope; class CustomBuilder extends EloquentBuilder implements UuidBuilderInterface { use BuilderUuidScope; public function whereActive() { return $this->where('active', 1); } }
Then simply return this in your models newEloquentBuilder
method.
class User extends Model implements Uuidable { use HasUuid; protected $fillable = ['name']; public function posts(): HasMany { return $this->hasMany(Post::class); } public function newEloquentBuilder($query): UuidBuilderInterface { return new CustomBuilder($query); } }