jetcod/eloquent-repository
最新稳定版本:1.6.0
Composer 安装命令:
composer require jetcod/eloquent-repository
包简介
This package is an implementation of repository pattern in laravel.
README 文档
README
Requirements
- PHP ^8.0 or higher
- Laravel 9.0 or higher
- Eloquent 8.0 or higher
Installation
You can install the library using Composer:
composer require jetcod/eloquent-repository
Usage
To use the library, you need to create a repository class for each of your Eloquent models. You can either extend the Jetcod\LaravelRepository\Eloquent\BaseRepository class to get started or use artisan command to generate the repository class:
php artisan make:repository UserRepository
Here's an example of a UserRepository class:
<?php namespace App\Repositories; use App\Models\User; use Illuminate\Database\Eloquent\Model; use Jetcod\LaravelRepository\Eloquent\BaseRepository class UserRepository extends BaseRepository { protected function getModelName() { return User::class; } }
In this example, the UserRepository extends the BaseRepository class and connects the repository to User mode by implementing getModelName() method . You can then use the repository to perform CRUD operations on the User model, like this:
<?php namespace App\Services; use App\Models\User; use Illuminate\Database\Eloquent\Model; use Jetcod\LaravelRepository\Eloquent\BaseRepository class UserService { protected $repository; protected function __construct(UserRepository $repository) { $this->repository = $repository; } public function getAdmins() { return $this->repository->findBy([ ['role', '=', 'ADMIN'] ]); } }
API
The following methods are available in the BaseRepository class:
find(int $id): Find a model by ID.findAll(): Get all models.findBy(array $conditions, array $relations = [], bool $paginate = true, int $pageSize = 10): Find all models and their identified relations by conditions. The result can ne either paginated or return as a collection.findOneBy(array $conditions, array $relations = []): Find a model and its identified relations by conditions.with($relations, $callback = null): Set the relationships that should be eager loaded.countBy(array $conditions): Count models by conditions.delete($model): Delete a modelinsert(array $rows): Bulk insert datacreate(array $attributes): Create a new model.updateOrCreate(array $data, array $conditions): Create or update a record matching the attributes, and fill it with valuesupdate(Model $model, array $data, array $fillable = []): Update an existing model.fill(Model $model, array $data, array $fillable = []): Fill the given model by the given array of data.delete($model): Delete a model.destroy($ids): Destroy an array or collection of ids.query(): Query builder instance.
You can also extend the BaseRepository * class to add custom methods for your specific use case.
Contributing
If you find any bugs or have ideas for new features, please open an issue or submit a pull request on GitHub.
License
My Eloquent Repository Library is open-sourced software licensed under the MIT license.
统计信息
- 总下载量: 42
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-02-17