一、安装

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 Round #325D (Div. 2) (DP)

    题目链接: D. Phillip and Trains 分析:dp 我们先初始化,dp[i]表示当前列第i行是否可达,r[i]表示上一个dp值,接下来从头搜到尾 如果该位置满足s[i+1]=='.'且 ...

  2. Activiti6.0教程 28张表解析 (三)

    使用Activit的朋友都知道Activiti对应的有28张表,今天我们就来说下Activit中28张表对应的含义是什么? 如何创建表? 在Activiti中创建表有三种方式,我们依次来看下: 一.通 ...

  3. 《windows核心编程系列》二十一谈谈基址重定位和模块绑定

    每个DLL和可执行文件都有一个首选基地址.它表示该模块被映射到进程地址空间时最佳的内存地址.在构建可执行文件时,默认情况下链接器会将它的首选基地址设为0x400000.对于DLL来说,链接器会将它的首 ...

  4. Solr中的group与facet的区别 [转]

    Solr中的group与facet的区别 facet 自己理解就是分组聚合用的, 如下说明 http://blog.csdn.net/a925907195/article/details/472572 ...

  5. LN : leetcode 123 Best Time to Buy and Sell Stock III

    lc 123 Best Time to Buy and Sell Stock III 123 Best Time to Buy and Sell Stock III Say you have an a ...

  6. Android IJKPlayer缓冲区设置以及播放一段时间出错解决方案

    IJKPlayer拖动播放进度会导致重新请求数据,未使用已经缓冲好的数据,所以应该尽量控制缓冲区大小,减少不必要的数据损失. mMediaPlayer.setOption(IjkMediaPlayer ...

  7. 微信小程序组件解读和分析:七、button按钮

    button按钮组件说明: button,顾名思义,按钮,类似于html的button标签.我们可以设置按钮的属性,比如字体颜色大小,背景颜色等,可以给按钮绑定事件,用户点击时会触发事件. butto ...

  8. 掌握Spark机器学习库-08.7-决策树算法实现分类

    数据集 iris.data 数据集概览 代码 package org.apache.spark.examples.examplesforml import org.apache.spark.Spark ...

  9. LigerUI用Post\Get\Ajax前后台交互方式的写法

    parms 参数统一 json格式的数据 url 访问后台的url 设置同步参数 [javascript] view plain copy   $.ajaxSetup({ async : false} ...

  10. Linq详细介绍

    声明----文档转载自:http://www.cnblogs.com/liulun/archive/2013/02/26/2909985.html 在说LINQ之前必须先说说几个重要的C#语言特性 一 ...