laravel l5-repository 使用

官方文档(译):https://segmentfault.com/a/1190000021690551

1、基本

1.1、l5-repository 是什么

laravel5 的一个扩展包,抽象数据库层,主要用于封装数据查询和存储逻辑。

2、安装

  • 安装
composer require prettus/l5-repository
  • 部署

laravel>=5.5,在框架的config/app.php中的providers数组添加如下代码

Prettus\Repository\Providers\RepositoryServiceProvider::class
  • 发布配置
php artisan vendor:publish --provider "Prettus\Repository\Providers\RepositoryServiceProvider
  • 命令
php artisan make:repository Post
# 为Post model生成一个repository层

php artisan make:criteria PostCriteria
# 创建Criteria(条件)

3、使用

3.1、Model

  • Models/Finance/ProfitModel.php
<?php
namespace App\Models\Finance;
use App\Models\BaseModel;
class ProfitModel extends BaseModel
{
    protected $table = 'profit';
    protected $guarded = ['id'];
    public $timestamps = false;
}

3.2、Repository

  • Repositories/Finance/ProfitRepository.php
<?php
namespace App\Repositories\Finance;
use App\Models\Finance\ProfitModel;
use Prettus\Repository\Eloquent\BaseRepository;
class ProfitRepository extends BaseRepository
{
    public function model()
    {
        return ProfitModel::class;
    }
}

3.3、Service中使用

  • Services/Finance/ProfitService.php
<?php
namespace App\Services\Finance;
use App\Repositories\Interfaces\Finance\ProfitRepository;
class ProfitService extends BaseService
{
    public $profitRepository;
    public function __construct(ProfitRepository $profitRepository)
    {
        $this->profitRepository = $profitRepository;
    }
    public function getAll()
    {
        return $this->profitRepository->all();
    }
}

4、Criteria-条件层

41、作用

复杂查询时,将查询条件封装起来,方便使用。

4.2、使用

  • Http/Criteria/Finance/ProfitLIstCriteria.php
<?php
namespace App\Http\Criteria\Finance;
use App\Http\Criteria\RequestCriteria;
use Prettus\Repository\Contracts\RepositoryInterface;
class ProfitLIstCriteria extends RequestCriteria
{
    public function apply($model, RepositoryInterface $repository)
    {
        $model = $model->when(
            $this->filled(['time_type', 'time_start']),
            function ($query) {
                $query->where($this->input('time_type'), '>=', $this->input('time_start'));
            }
        )
            ->when(
                $this->filled(['time_type', 'time_end']),
                function ($query) {
                    $query->where($this->input('time_type'), '<=', $this->input('time_end'));
                }
            );
        return $model->orderByDesc('id');
    }
}
  • Services/Finance/ProfitService.php
public function getList()
{
    try {
        //通过Criteria获取结果
        $data = $this->profitRepository->pushCriteria(app(ProfitLIstCriteria::class))->all();
        return $data;
    } catch (RepositoryException $e) {
    }
}
  • criteria 更多详情使用方法查看官方文档。

5、常用方法

#通过Repository获取所有结果
$posts = $this->repository->all();

#通过Repository获取分页结果
$posts = $this->repository->paginate($limit = null, $columns = ['*']);

#通过id获取结果
$post = $this->repository->find($id);

#隐藏Model的属性
$post = $this->repository->hidden(['country_id'])->find($id);

#显示Model指定属性
$post = $this->repository->visible(['id', 'state_id'])->find($id);

#加载Model关联关系
$post = $this->repository->with(['state'])->find($id);

#根据字段名称获取结果
$posts = $this->repository->findByField('country_id','15');

#根据多个字段获取结果
$posts = $this->repository->findWhere([
    //Default Condition =
    'state_id'=>'10',
    'country_id'=>'15',
    //Custom Condition
    ['columnName','>','10']
]);

#根据某一字段的多个值获取结果
$posts = $this->repository->findWhereIn('id', [1,2,3,4,5]);

#获取不包含某一字段的指定值的结果
$posts = $this->repository->findWhereNotIn('id', [6,7,8,9,10]);

#通过自定义scope获取结果
$posts = $this->repository->scopeQuery(function($query){
    return $query->orderBy('sort_order','asc');
})->all();

#在`Repository`中创建数据
$post = $this->repository->create( Input::all() );

#在`Repository`中更新数据
$post = $this->repository->update( Input::all(), $id );

#在`Repository`中删除数据
$this->repository->delete($id)

#在`Repository`中通过多字段删除数据
$this->repository->deleteWhere([
    'state_id'=>'10',
    'country_id'=>'15',
])
posted @ 2022-08-09 14:26  pine007  阅读(559)  评论(0编辑  收藏  举报