l5-repository基本使用
一、安装
composer require prettus/l5-repository

二、Model层:Warehouse.php
<?php namespace App\Model; use Illuminate\Database\Eloquent\Model; class Warehouse extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [];
protected $table = "supplier_warehouse";
protected $primaryKey = 'g_w_id';
public $timestamps = false;
}
三、仓库的两个文件:
3.1、文件及位置

3.2、WarehouseRepository.php:
<?php namespace App\Repositories; use Prettus\Repository\Contracts\RepositoryInterface; /**
* Interface ShopRepository.
*
* @package namespace App\Repositories;
*/
interface WarehouseRepository extends RepositoryInterface
{
public function getIdsByCmpId($cmpId, $sel);
public function getNameById($g_w_id, $sel);
}
3.3、WarehouseRepositoryEloquent.php:
<?php namespace App\Repositories; use App\Model\Warehouse;
use Illuminate\Support\Facades\DB;
use Prettus\Repository\Eloquent\BaseRepository; /**
* Class ShopRepositoryEloquent.
*
* @package namespace App\Repositories;
*/
class WarehouseRepositoryEloquent extends BaseRepository implements WarehouseRepository
{
/**
* Specify Model class name
*
* @return string
*/
public function model()
{
return Warehouse::class;
} public function getIdsByCmpId($cmpId, $sel)
{
return $this->model
->select(DB::raw(implode(',',$sel)))
->where('company_id',$cmpId)
->get();
} public function getNameById($g_w_id, $sel)
{
return $this->model
->select(DB::raw(implode(',',$sel)))
->where('g_w_id',$g_w_id)
->get();
}
}
四、绑定:
4.1、文件及位置:

4.2、RepositoryServiceProvider.php:
$this->app->bind(\App\Repositories\WarehouseRepository::class, \App\Repositories\WarehouseRepositoryEloquent::class);
五、services
5.1、文件及位置:

5.2、WarehouseService.php:
<?php namespace App\Services; class WarehouseService
{
private $warehouseRepository; public function __construct($warehouseRepository)
{
$this->warehouseRepository = $warehouseRepository;
} public function getIdsByCmpId($cmpId)
{
return $this->warehouseRepository->getIdsByCmpId($cmpId, ['g_w_id']);
} public function getNameById($g_w_id)
{
return $this->warehouseRepository->getNameById($g_w_id, ['warehouse_name']);
}
}
六、控制器中调用:
6.1、文件及位置

6.2、WarehouseController.php
<?php namespace App\Http\Controllers\Warehouse; use App\Repositories\WarehouseRepository;
use App\Services\WarehouseService;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller; class WarehouseController extends Controller
{
private $warehouseService; public function __construct(WarehouseRepository $warehouseRepository)
{
$this->warehouseService = new WarehouseService($warehouseRepository);
} public function test()
{
$cmpId = ;
$listWarehouse = $this->warehouseService->getIdsByCmpId($cmpId);
}
}
七、在config/app.php上加载组件:

代码:
App\Providers\RepositoryServiceProvider::class,
其他随笔l5-repository基本使用--结合使用artisan
l5-repository基本使用的更多相关文章
- DDD 领域驱动设计-谈谈 Repository、IUnitOfWork 和 IDbContext 的实践(3)
上一篇:<DDD 领域驱动设计-谈谈 Repository.IUnitOfWork 和 IDbContext 的实践(2)> 这篇文章主要是对 DDD.Sample 框架增加 Transa ...
- Asp.Net Core + Dapper + Repository 模式 + TDD 学习笔记
0x00 前言 之前一直使用的是 EF ,做了一个简单的小项目后发现 EF 的表现并不是很好,就比如联表查询,因为现在的 EF Core 也没有啥好用的分析工具,所以也不知道该怎么写 Linq 生成出 ...
- windows 部署 git 服务器报 Please make sure you have the correct access rights and the repository exists.错误
这两天在阿里云上弄windows 服务器,顺便部署了一个git服务.根据网上教程一步步操作下来,最后在 remote远程仓库的时候提示 fatal: 'yourpath/test.git' does ...
- 初探领域驱动设计(2)Repository在DDD中的应用
概述 上一篇我们算是粗略的介绍了一下DDD,我们提到了实体.值类型和领域服务,也稍微讲到了DDD中的分层结构.但这只能算是一个很简单的介绍,并且我们在上篇的末尾还留下了一些问题,其中大家讨论比较多的, ...
- Repository 仓储,你的归宿究竟在哪?(三)-SELECT 某某某。。。
写在前面 首先,本篇博文主要包含两个主题: 领域服务中使用仓储 SELECT 某某某(有点晕?请看下面.) 上一篇:Repository 仓储,你的归宿究竟在哪?(二)-这样的应用层代码,你能接受吗? ...
- Failure to find xxx in xxx was cached in the local repository, resolution will not be reattempted until the update interval of nexus has elapsed or updates are forced @ xxx
问题: 在linux服务器上使用maven编译war时报错: 16:41:35 [FATAL] Non-resolvable parent POM for ***: Failure to find * ...
- DDD 领域驱动设计-谈谈 Repository、IUnitOfWork 和 IDbContext 的实践(2)
上一篇:<DDD 领域驱动设计-谈谈 Repository.IUnitOfWork 和 IDbContext 的实践(1)> 阅读目录: 抽离 IRepository 并改造 Reposi ...
- 【无私分享:ASP.NET CORE 项目实战(第五章)】Repository仓储 UnitofWork
目录索引 [无私分享:ASP.NET CORE 项目实战]目录索引 简介 本章我们来创建仓储类Repository 并且引入 UnitOfWork 我对UnitOfWork的一些理解 UnitOfW ...
- CentOS系统yum源使用报错:Error: Cannot retrieve repository metadata (repomd.xml) for repository: rpmforge.
服务器上的yum突然不好使用,使用yum的时候报错如下:[root@bastion-IDC src]# yum list......Could not retrieve mirrorlist http ...
- Laravel与Repository Pattern(仓库模式)
为什么要学习Repository Pattern(仓库模式) Repository 模式主要思想是建立一个数据操作代理层,把controller里的数据操作剥离出来,这样做有几个好处: 把数据处理逻辑 ...
随机推荐
- IDEA Terminal
如果IDEA在Terminal中无法使用环境变量,提示没有此命令的话,请查看在cmd中是否可以正常使用,然后,请右击IDEA以管理员方式进行运行,Terminal就可以正常使用了
- PCB 合拼遍历(全排序+旋转90度) 基本遍历方法
分享一下PCB合拼的组合的遍历方法,在分享之前先纠正一下 PCB拼板之多款矩形排样算法实现--学习 时间复杂度计算错误 一.PCB 合拼(全排序+旋转90度)的时间复杂度是多少? 二.合拼遍历(全 ...
- Luogu P1280 Niko的任务【线性dp】By cellur925
Nikonikoni~~ 题目传送门 这是当时学长讲dp的第一道例题,我还上去献了个丑,然鹅学长讲的方法我似董非董(??? 我当时说的怎么设计这道题的状态,但是好像说的是二维,本题数据范围均在1000 ...
- c语言中的rand()函数用法
rand() rand()函数作用:用来产生随机数,但是,rand()的内部实现是用线性同余法实现的,是伪随机数,由于周期较长,因此在一定范围内可以看成是随机的. rand()函数返回:返回一个范围在 ...
- Bootstrap简介及安装使用
Bootstrap 简介 什么是 Bootstrap? Bootstrap 是一个用于快速开发 Web 应用程序和网站的前端框架.Bootstrap 是基于 HTML.CSS.JAVASCRIPT 的 ...
- ambari集群里如何正确删除历史修改记录(图文详解)
不多说,直接上干货! 答:这些你想删除的话得得去数据库里删除,最好别删除 . 现在默认就是使用好的配置 欢迎大家,加入我的微信公众号:大数据躺过的坑 人工智 ...
- 利用Laravel 搭建oauth2 API接口 附 Unauthenticated 解决办法
利用Laravel 搭建oauth2 API接口 要求 laravel 5.4以上 安装 $ composer require laravel/passport 在配置文件 config/app.ph ...
- SugarCRM安装踩雷(一)
安装SugarCRM前置条件: 1.找对平台.正确版本的安装包 2.APACHE + MYSQL + TOMCAT环境先确保OK 坑1: 进入安装参数设置步骤的MYSQL用户密码——这里根据Mysql ...
- 浅谈网上的zoomlistview存在的问题
最近项目主要是做一个类似wps文档阅历的功能,以列表的形式显示文档,并且需要实现缩放平移.而网上关于此类功能的实现主要是通过自定义的listview实现的,类名为ZoomListView. 网上的zo ...
- 富文本KindEditor使用
1.官网down KindEditor,添加到自己的项目中:添加时可把不需要的文件夹干掉,asp/php等等.我的项目用的是纯html和js,直接调用后台api: 2.页面引入相关js.eclipse ...