一、从头开始创建

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. bzoj 4622: [NOI 2003] 智破连环阵【dfs+匈牙利算法】

    一个炸弹炸一个区间的武器,想到二分图匹配 但是直接dfs断点显然不行,预处理出dis[i]为i到m的至多值来最优性剪枝,并且标记ok[i][j]为炸弹i可以炸到j武器,mx[i][j]为i炸弹从j武器 ...

  2. UVA1328 Period【KMP/周期串/循环节】By cellur925

    鲜有的在luogu发题解以及使用LaTex??? 就丢链接跑了.

  3. android 启动报错

    报错如下: AAPT err(Facade for 1532009679): libpng error: Read Error Error:Execution failed for task ':ap ...

  4. Pursuit For Artifacts CodeForces - 652E

    https://vjudge.net/problem/CodeForces-652E 边双啊,就是点双那个tarjan里面,如果low[v]==dfn[v](等同于low[v]>dfn[u]), ...

  5. selenium处理的操作

  6. adb的含义

    ADB全名Andorid Debug Bridge. 是一个Debug工具.为何称之为Bridge呢?因为adb是一个标准的C/S结构的工具, 是要连接开发电脑和调试手机的.包含如下几个部分: 1.C ...

  7. page.php 引入js文件

    2种写法 <script type='text/javascript' src='<?php echo get_template_directory_uri().'/js/jquery-1 ...

  8. 使用grunt构建前端项目

    1. grunt构建工具是基于nodejs上的,所以在使用之前一定要先安装好nodejs 2. 安装好nodejs后,node -v查看node版本 npm-v 查看npm版本信息 3. 在需要用到的 ...

  9. AJPFX实现兔子问题

    有一对小兔子,从第三个月长成开始每个月生一对小兔子,新出生的小兔子从第三个月长成开始每个月也生一对小兔子,假设所有的兔子都不会死,问每个月兔子的总数?(月数可以是6,12).大神看看我笨方法谢的对吗? ...

  10. Docker容器相关技术

    docker需要依赖的Linux内核特性:(1)Namespaces 命名空间PID(Process ID) 用来隔离进程NET(Network) 管理网络接口IPC(InterProcess com ...