一、从头开始创建

1、执行以下artisan:

php artisan make:entity Student

如果某个文件已经存在,则不会创建新的文件去覆盖原有的文件,案例如下:

2、修改model:Student.php

    protected $table = 'student';
protected $primaryKey = 'id';
public $timestamps = false;
protected $fillable = [];

3、StudentRepository.php增加方法:

public function getInfoById($id, $sel);

4、StudentRepositoryEloquent.php添加方法:

use Illuminate\Support\Facades\DB;
    public function getInfoById($id, $sel)
{
return $this->model
->select(DB::raw(implode(',', $sel)))
->where('id', $id)
->first();
}

5、新增StudentService.php

<?php

namespace App\Services;

class StudentService
{
private $studentRepository; public function __construct($studentRepository)
{
$this->studentRepository = $studentRepository;
} public function getInfoById($id)
{
return $this->studentRepository->getInfoById($id, ['name', 'age']);
}
}

6、修改控制器:StudentsController.php

<?php

namespace App\Http\Controllers;

use App\Repositories\StudentRepository;
use App\Services\StudentService; /**
* Class StudentsController.
*
* @package namespace App\Http\Controllers;
*/
class StudentsController extends Controller
{
private $studentService;
/**
* StudentsController constructor.
*
* @param StudentRepository $repository
* @param StudentValidator $validator
*/
public function __construct(StudentRepository $studentRepository)
{
$this->studentService = new StudentService($studentRepository);
} public function test()
{
$data = $this->studentService->getInfoById();
dd($data);
}
}

7、然后添加必要的路由,即可获得数据

Route::get('/student/test', 'StudentsController@test');

8、注册RepositoryServiceProvider:

其他随笔l5-repository基本使用

l5-repository基本使用--结合使用artisan的更多相关文章

  1. laravel 开发辅助工具

    laravel 开发辅助工具 配置 添加服务提供商 将下面这行添加至 config/app.php 文件 providers 数组中: 'providers' => [ ... App\Plug ...

  2. laravel 5.0 artisan 命令列表(中文简体)

    #php artisan list Laravel Framework version Usage: [options] command [arguments] Options(选项): --help ...

  3. Lavarel artisan 命令

    [alex@iZ25c5aeyiiZ yiqizou3.0]# php artisan list Laravel Framework version Usage: command [options] ...

  4. Lumen Repository(仓储)

    在 Laravel 5 中使用 Repository 模式实现业务逻辑和数据访问的分离:http://laravelacademy.org/post/3063.html Eloquent: 集合:ht ...

  5. Laravel Repository Pattern

    Laravel Repository Pattern   The Repository Pattern can be very helpful to you in order to keep your ...

  6. Laravel 中使用 Repository 模式

    在本文中,我会向你展示如何在 Laravel 中从头开始实现 repository 设计模式.我将使用 Laravel 5.8.3 版,但 Laravel 版本不是最重要的.在开始写代码之前,你需要了 ...

  7. L5 Swagger 使用说明

    网上看了看,关于这个扩展介绍很少.今天工作恰好用到,研究了一下,觉得有必要分享一下. 一.  简介: 这个包是Swagger-php和Swagger-ui的封装,适用于Laravel5. 二.版本要求 ...

  8. Laravel artisan 命令

    获取命令列表 php artisan Laravel Framework 7.26.0 Usage: command [options] [arguments] Options: -h, --help ...

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

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

随机推荐

  1. python __builtins__ 函数

    dir(__builtins__) 1.'abs', 对传入参数取绝对值 abs(x, /) Return the absolute value of the argument. >>&g ...

  2. Vue 基础指令学习

    学习Vue的一些总结,第一次写博客,文笔实在是很差 不过我会不断写的. <template> <!--只能有一个根节点 --> <div> <pre> ...

  3. JSP | 基础 | 加载类失败:com.mysql.jdbc.Driver

    两个原因: 1. 连接数据库需要的jar包没有导入Tomcat的lib库中 解决方案: 打开Tomcat的安装目录下的lib文件夹,把jar包拖进lib库后,重启tomcat服务器 2.  mysql ...

  4. Qt基本应用

    1 使用方式 在qt designer中直接设计图形界面,然后使用pyGUI转换成py文件. 可以发现,转换的文件为一个class.并不是一个完整的程序(运行时无法出现窗口).这个类名字是Ui_Mai ...

  5. Magic Numbers CodeForces - 628D

    Magic Numbers CodeForces - 628D dp函数中:pos表示当前处理到从前向后的第i位(从1开始编号),remain表示处理到当前位为止共产生了除以m的余数remain. 不 ...

  6. Problem D: 勤奋的涟漪2 dp + 求导

    http://www.gdutcode.sinaapp.com/problem.php?cid=1049&pid=3 dp[i][state]表示处理了前i个,然后当前状态是state的时候的 ...

  7. Java-每日编程练习题③

    一.计算圆周率 中国古代数学家研究出了计算圆周率最简单的办法: PI=4/1-4/3+4/5-4/7+4/9-4/11+4/13-4/15+4/17...... 这个算式的结果会无限接近于圆周率的值, ...

  8. currentStyle getComputedStyle兼容

    function getStyle(obj,attr){ if(obj.currentStyle) {return obj.currentStyle[attr]} else{ return getCo ...

  9. SPI总线小结

    串行外设接口(Serial Peripheral Interface,SPI)的缩写.是一种高速的,全双工,同步的通信总线,并且在芯片的管脚上只占用四根线.Motorola首先在其MC68HCXX系列 ...

  10. MyBatis学习(四)

    前言 最近比较松懈,下班回家后也懒得学习了.今晚实在是看不下去了,争取时间学习.社会上有这么多的资源,就看谁能抢的多吧.今天就说说MyBatis的动态SQL吧 正文 动态 SQL 通常要做的事情是有条 ...