laravel l5-repository 使用
官方文档(译):https://segmentfault.com/a/1190000021690551
1、基本
1.1、l5-repository 是什么
laravel5 的一个扩展包,抽象数据库层,主要用于封装数据查询和存储逻辑。
2、安装
- 安装
copycomposer require prettus/l5-repository
- 部署
laravel>=5.5,在框架的config/app.php
中的providers
数组添加如下代码
copyPrettus\Repository\Providers\RepositoryServiceProvider::class
- 发布配置
copyphp artisan vendor:publish --provider "Prettus\Repository\Providers\RepositoryServiceProvider
- 命令
copyphp artisan make:repository Post
# 为Post model生成一个repository层
php artisan make:criteria PostCriteria
# 创建Criteria(条件)
3、使用
3.1、Model
- Models/Finance/ProfitModel.php
copy<?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
copy<?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
copy<?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
copy<?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
copypublic function getList()
{
try {
//通过Criteria获取结果
$data = $this->profitRepository->pushCriteria(app(ProfitLIstCriteria::class))->all();
return $data;
} catch (RepositoryException $e) {
}
}
criteria
更多详情使用方法查看官方文档。
5、常用方法
copy#通过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',
])
分类:
laravel
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构