blok / laravel-repository
3.0.4
2022-05-12 06:46 UTC
Requires
- php: >=7.0
- blok/utils: *
- illuminate/support: >5.5
Requires (Dev)
- orchestra/testbench: ~3.6.0
- phpunit/phpunit: ~7.0
README
通过Repository模式处理业务逻辑的观点方式。
此包倾向于为您提供一个观点结构,以在单个仓库文件夹内处理业务逻辑,而不是在控制器、播种器等地方重复代码。
它附带了一些方便的助手,让您可以在控制器、API控制器或GraphQL变异中使用此仓库,无需每次都重新定义轮子。
安装
通过Composer安装
composer require blok/laravel-repository
用法
Blok仓库是一个Laravel包,将为您的模型和控制提供额外功能。
创建仓库类
php artisan make:repository UserRepository
它将在/app/Repositories/UserRepository中创建一个仓库类
<?php
namespace App\Repositories;
use App\User;
use Blok\Repository\AbstractEloquentRepository;
class UsersRepository extends AbstractEloquentRepository
{
function model()
{
return User::class;
}
}
无需任何配置,它将处理基本的CRUD操作
- all
- find
- findBy
- create
- update
- delete
- getForm
- 验证
当然,您可以在此仓库类中覆盖这些方法中的任何一种,并创建自己的方法
如何在控制器中使用它?
Blok/Repository附带一个非常方便且常见的ApiController结构。要使用它,您可以做
It will create an ApiController inside App/Http/Controllers/Api :
````
<?php
namespace App\Http\Controllers\Api;
use App\Repositories\UsersRepository;
use Blok\Repository\Http\Controllers\AbstractApiController;
class UserController extends AbstractApiController
{
function model()
{
return UsersRepository::class;
}
}
````
This controller will handle directly the CRUD logic of your repository for more infos see AbstractApiController
### How to use it in Graphql ?
Blok/Repository comes with a very handy and common Graphql Mutation structure. It assumes that you use [https://lighthouse-php.com/](https://lighthouse-php.com/) to handle GraphQL inside Laravel.
To use is you can do :
````php artisan make:mutation UserUpdateMutation````
It will create a graphql mutation inside App/Graphql/Mutations :
````
<?php
namespace App\GraphQL\Mutations;
class UserUpdateMutation extends AbstractUpdateMutation
{
function repository()
{
return \App\Repositories\UserRepository::class;
}
}
````
This controller will handle directly the CRUD logic mutation for Create, Update and Delete.
### Adding a business logic with a Criteria class
Off course, any methods of the AbstractClass can be overriden but sometimes you just need to add somewhere else your own query logic to reuse it. For that we implemented a usefull patern that will help to keep your query logic in a separated class reusable anywhere.
Let's give a simple exemple for the get all methods => you want to get only public users.
### Create a Criteria
``` php artisan make:criteria OnlyPublicCriteria ```
This will create a class inside /app/Repositories/Criterias/OnlyPublicCriteria
```
<?php
namespace App\Repositories\Criterias;
use Blok\Repository\AbstractCriteria;
class OnlyPublicCriteria extends AbstractCriteria
{
public $type = "public";
public function __construct($type = 'public'){
$this->type = $type;
}
public function apply($model, $repository = null)
{
return $model->where('visibility', $this->type);
}
}
```
### Use it inside your Repository
In your UserRepository, you can add and handle your criteria like that :
```
public function all($columns = array('*'))
{
if (!auth()->check()) {
$this->pushCriteria(new OnlyPublicCriteria());
}
return parent::all($columns);
}
```
It will apply the condition of where visibility=public automatically to the $userRepository->all() method.
### Use it inside your Controller
In your ControllerClass, you can inject this param like that :
```
<?php
namespace App\Http\Controllers;
use App\Repositories\UserRepository;
use Illuminate\Http\Request;
class UserController extends Controller
{
/**
* @var UserRepository
*/
private $userRepository;
public function __construct(UserRepository $userRepository)
{
$this->userRepository = $userRepository;
}
/**
* Display a listing of the resource.
*
* @param Request $request
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
if(!auth()->check()){
$this->userRepository->pushCriteria(new OnlyPublicCriteria('public'));
}
$users = $this->userRepository->paginate(12, $request->all());
return view('users', compact('users'));
}
}
```
It will have the same behavior but at the Controller level. Off course, you are free to add your variables logic when you initiate the criteria (for exemple here I push the type public for the demo).
Putting this logic inside a Criteria, will help you to queue your query condition and reuse it in different Repository.
### Criterias available by default:
You have criterias helpers available in the namespace Blok\Repository\Criterias.
They each contain an argument containing your filters, here are more explanations for some of them :
#### WhereCriteria:
This criteria is used to filter the data coming from the source model.
For the syntax, separate the elements with a space, you can use all the classical operators except BETWEEN which is not implemented yet. You can also use AND and OR with && and ||.
Example:
```
$filters = "first_name = sarah || first_name = mario || first_name LIKE %deb% && email LIKE %gmail% || email LIKE %outlook% && phone != null";
```
#### WhereHasCriteria:
This criteria works like WhereCriteria with the difference that it is there to filter elements of the models related to the source model.
For the syntax it's the same as WhereCriteria but you have to put the relation for which the filters are applied followed by a "->" (without space) at the beginning of the query and after each &&.
You can use the || but it will be applied to the filtered table (selected with "relationshipName->".
Example:
```
$filters = "bookings->price_ht > 1000 || price_ht < 100 && socialUsers->provider = google";
```
## Security
If you discover any security related issues, please [email me](daniel@cherrypulp.com) instead of using the issue tracker.
## Credits
- [Daniel Sum](https://github.com/cherrylabs/blok-repository)
- [All contributors](https://github.com/cherrylabs/blok-repository/graphs/contributors)
This package is bootstrapped with the help of
[blok/laravel-package-generator](https://github.com/cherrylabs/blok-laravel-package-generator).