um-flint / laravel-repository
A repository pattern for Laravel.
Installs: 1 972
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 3
Forks: 0
Open Issues: 0
Requires
- php: ^8.1
- illuminate/database: ^8.0|^9.0
- illuminate/pagination: ^8.0|^9.0
- illuminate/support: ^8.0|^9.0
- illuminate/validation: ^8.0|^9.0
This package is not auto-updated.
Last update: 2024-11-13 03:58:12 UTC
README
Methods
UMFlint\Repository\Contracts\RepositoryInterface
- lists($column, $key = null)
- pluck($column, $key = null)
- sync($id, $relation, $attributes, $detaching = true)
- syncWithoutDetaching($id, $relation, $attributes)
- all($columns = ['*'])
- paginate($limit = null, $columns = ['*'])
- simplePaginate($limit = null, $columns = ['*'])
- find($id, $columns = ['*'])
- findByField($field, $value, $columns = ['*'])
- findWhere(array $where, $columns = ['*'])
- findWhereIn($field, array $values, $columns = ['*'])
- findWhereNotIn($field, array $values, $columns = ['*'])
- create(array $attributes)
- update(array $attributes, $id)
- delete($id)
- orderBy($column, $direction = 'asc')
- has($relation)
- with($relations)
- whereHas($relation, $closure)
- withCount($relations)
- hidden(array $fields)
- visible(array $fields)
- scopeQuery(\Closure $scope)
- resetScope()
Traits
UMFlint\Traits\SoftDeletes
This trait allows for soft deletes:
- delete($id, $force = false)
In order to query for deleted entities:
- withTrashed()
To restore a soft deleted entity:
- restore($id)
Hooks
Boot
Anything that needs to happen when the repository class is created. Uses Laravels container for automatic injection.
- boot()
Create
- beforeCreate(&$attributes)
- afterCreate($model, &$attributes)
Update
- beforeUpdate($model, &$attributes)
- afterUpdate($model, &$attributes)
Delete
- beforeDelete($model)
- afterDelete($model, $deleted)
Restore
- beforeRestore($model)
- afterRestore($model)
Validation
Example rules:
<?php namespace App\Rules; use App\Models\Announcement; use Illuminate\Validation\Rule; use UMFlint\Repository\Rules\BaseRules; class AnnouncementRules extends BaseRules { protected function rules(): array { return [ 'type' => ['required', Rule::in(array_keys(Announcement::$types))], 'title' => 'required', 'message' => 'required', 'start_at' => ['required', 'date'], 'end_at' => ['required', 'date'], ]; } }
To use it in a repository:
<?php namespace App\Repositories; use App\Models\Announcement; use App\Rules\AnnouncementRules; use UMFlint\Repository\BaseRepository; class AnnouncementRepository extends BaseRepository { public function model(): string { return Announcement::class; } public function rules(): array { return AnnouncementRules::getRules(); } }