一、安装

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基本使用的更多相关文章

  1. DDD 领域驱动设计-谈谈 Repository、IUnitOfWork 和 IDbContext 的实践(3)

    上一篇:<DDD 领域驱动设计-谈谈 Repository.IUnitOfWork 和 IDbContext 的实践(2)> 这篇文章主要是对 DDD.Sample 框架增加 Transa ...

  2. Asp.Net Core + Dapper + Repository 模式 + TDD 学习笔记

    0x00 前言 之前一直使用的是 EF ,做了一个简单的小项目后发现 EF 的表现并不是很好,就比如联表查询,因为现在的 EF Core 也没有啥好用的分析工具,所以也不知道该怎么写 Linq 生成出 ...

  3. windows 部署 git 服务器报 Please make sure you have the correct access rights and the repository exists.错误

    这两天在阿里云上弄windows 服务器,顺便部署了一个git服务.根据网上教程一步步操作下来,最后在 remote远程仓库的时候提示 fatal: 'yourpath/test.git' does ...

  4. 初探领域驱动设计(2)Repository在DDD中的应用

    概述 上一篇我们算是粗略的介绍了一下DDD,我们提到了实体.值类型和领域服务,也稍微讲到了DDD中的分层结构.但这只能算是一个很简单的介绍,并且我们在上篇的末尾还留下了一些问题,其中大家讨论比较多的, ...

  5. Repository 仓储,你的归宿究竟在哪?(三)-SELECT 某某某。。。

    写在前面 首先,本篇博文主要包含两个主题: 领域服务中使用仓储 SELECT 某某某(有点晕?请看下面.) 上一篇:Repository 仓储,你的归宿究竟在哪?(二)-这样的应用层代码,你能接受吗? ...

  6. 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 * ...

  7. DDD 领域驱动设计-谈谈 Repository、IUnitOfWork 和 IDbContext 的实践(2)

    上一篇:<DDD 领域驱动设计-谈谈 Repository.IUnitOfWork 和 IDbContext 的实践(1)> 阅读目录: 抽离 IRepository 并改造 Reposi ...

  8. 【无私分享:ASP.NET CORE 项目实战(第五章)】Repository仓储 UnitofWork

    目录索引 [无私分享:ASP.NET CORE 项目实战]目录索引 简介 本章我们来创建仓储类Repository 并且引入 UnitOfWork 我对UnitOfWork的一些理解  UnitOfW ...

  9. 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 ...

  10. Laravel与Repository Pattern(仓库模式)

    为什么要学习Repository Pattern(仓库模式) Repository 模式主要思想是建立一个数据操作代理层,把controller里的数据操作剥离出来,这样做有几个好处: 把数据处理逻辑 ...

随机推荐

  1. CodeForces 722B Verse Pattern (水题)

    题意:统计元音,这里多加一个元音,y. 析:直接统计就好了. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000" ...

  2. 初窥MySQL性能调优

    本文涉及:MySQL自带的性能测试工具mysqlslap的使用及几个性能调优的方法 性能测试工具—mysqlslap mysqlslap是MySQL自带的一款非常优秀的性能测试工具.使用它可以 模拟多 ...

  3. Luogu P1195/P1892 口袋的天空/BOI团伙 【最小生成树/并查集】By cellur925

    其实这俩题挺水的,团伙拿下了一血,但是感觉还是写一下博客比较好x. 一.团伙 题目描述 1920年的芝加哥,出现了一群强盗.如果两个强盗遇上了,那么他们要么是朋友,要么是敌人.而且有一点是肯定的,就是 ...

  4. 关于新手html的认识 以及对table的基本用法

    1.html语言 <!DOCTYPE html> 声明html <!--双标签--> <!--<html 属性名="属性值 " 属性名2=&qu ...

  5. [POI2007]大都市meg

    Description 在经济全球化浪潮的影响下,习惯于漫步在清晨的乡间小路的邮递员Blue Mary也开始骑着摩托车传递邮件了.不过,她经常回忆起以前在乡间漫步的情景.昔日,乡下有依次编号为1..n ...

  6. Linux环境下使用yum安装zip和unzip

    Linux环境下使用yum安装zip和unzip. yum install zip yum install unzip

  7. java数组实现买彩票(二个一维数组的比较思想)

    /** 设计一个程序,模拟从彩球池里随机抽取5个彩球(彩球池里一共有11个彩球,编号为1~11), 要求在控制台打印出这5个被取出来的彩球的编号(注意编号不能重复). 思路: 1.创建一个int类型的 ...

  8. oracle (DBaaS) 服务介绍

    转 https://oracle-base.com/articles/vm/oracle-cloud-database-as-a-service-dbaas-create-service?utm_so ...

  9. 转 【TTS】AIX平台数据库迁移到Linux--基于RMAN(真实环境)

    [TTS]AIX平台数据库迁移到Linux--基于RMAN(真实环境) http://www.cnblogs.com/lhrbest/articles/5186933.html 各位技术爱好者,看完本 ...

  10. jsp的简介

    https://www.w3cschool.cn/jsp/jsp-intro.html