一、从头开始创建

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. 一篇文章搞定面试中的二叉树题目(java实现)

    最近总结了一些数据结构和算法相关的题目,这是第一篇文章,关于二叉树的. 先上二叉树的数据结构: class TreeNode{ int val; //左孩子 TreeNode left; //右孩子 ...

  2. 【CodeForces - 546C】Soldier and Cards (vector或队列)

    Soldier and Cards 老样子,直接上国语吧  Descriptions: 两个人打牌,从自己的手牌中抽出最上面的一张比较大小,大的一方可以拿对方的手牌以及自己打掉的手牌重新作为自己的牌, ...

  3. Zeppelin的入门使用系列之创建新的Notebook

    前期博客 hadoop-2.6.0.tar.gz + spark-1.6.1-bin-hadoop2.6.tgz + zeppelin-0.5.6-incubating-bin-all.tgz(mas ...

  4. iOS 优雅地隐藏导航栏NavigationBar (Objc)

    @interface FSViewController () <UINavigationControllerDelegate> @end @implementation FSViewCon ...

  5. iOS UITableView reloadData 刷新结束后执行后续操作

    如果在reloadData后需要立即获取tableview的cell.高度,或者需要滚动tableview. 如果直接在reloadData后执行代码是有可能出问题的,比如indexPath为nil等 ...

  6. java hashCode 作用

    hashCode 作用,对象根据hashCode的值分区域存放 /** * hashCode 作用 * * @author Administrator * */ public class Point ...

  7. python中的栈

    # @File: stack # 列表实现栈 class MyStack(object): def __init__(self): self.data = [] def push(self, item ...

  8. Jumping on Walls CodeForces - 198B

    Jumping on Walls CodeForces - 198B 应该是一个隐式图的bfs,或者叫dp. 先是一个TLE的O(nklogn) #include<cstdio> #inc ...

  9. Ansj分词的使用

    jar包下载地址:http://download.csdn.net/download/jj12345jj198999/6020541 博客地址:http://blog.csdn.net/a822631 ...

  10. 8.bootstrap下拉菜单、按钮组、按钮式下拉菜单

    下拉菜单 dropdown 对齐方式: .dropdown-menu-right     .dropdown-menu-left <div class="container" ...