l5-repository基本使用--结合使用artisan
一、从头开始创建
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基本使用--结合使用artisan的更多相关文章
- laravel 开发辅助工具
		laravel 开发辅助工具 配置 添加服务提供商 将下面这行添加至 config/app.php 文件 providers 数组中: 'providers' => [ ... App\Plug ... 
- laravel 5.0 artisan 命令列表(中文简体)
		#php artisan list Laravel Framework version Usage: [options] command [arguments] Options(选项): --help ... 
- Lavarel artisan 命令
		[alex@iZ25c5aeyiiZ yiqizou3.0]# php artisan list Laravel Framework version Usage: command [options] ... 
- Lumen Repository(仓储)
		在 Laravel 5 中使用 Repository 模式实现业务逻辑和数据访问的分离:http://laravelacademy.org/post/3063.html Eloquent: 集合:ht ... 
- Laravel Repository Pattern
		Laravel Repository Pattern The Repository Pattern can be very helpful to you in order to keep your ... 
- Laravel 中使用 Repository 模式
		在本文中,我会向你展示如何在 Laravel 中从头开始实现 repository 设计模式.我将使用 Laravel 5.8.3 版,但 Laravel 版本不是最重要的.在开始写代码之前,你需要了 ... 
- L5 Swagger 使用说明
		网上看了看,关于这个扩展介绍很少.今天工作恰好用到,研究了一下,觉得有必要分享一下. 一. 简介: 这个包是Swagger-php和Swagger-ui的封装,适用于Laravel5. 二.版本要求 ... 
- Laravel artisan 命令
		获取命令列表 php artisan Laravel Framework 7.26.0 Usage: command [options] [arguments] Options: -h, --help ... 
- DDD 领域驱动设计-谈谈 Repository、IUnitOfWork 和 IDbContext 的实践(3)
		上一篇:<DDD 领域驱动设计-谈谈 Repository.IUnitOfWork 和 IDbContext 的实践(2)> 这篇文章主要是对 DDD.Sample 框架增加 Transa ... 
随机推荐
- IT兄弟连 JavaWeb教程 Servlet 状态管理 会话跟踪
			HTTP协议是无状态的,我们的客户端与服务器的每一次请求与响应,我们服务器都没有记忆能力将客户端与服务器的多次交互数据进行存储与管理共有两种技术实现: ● 基于客户端实现:Cookie,将状态保存在 ... 
- influxdb数据库的安装部署(windows)二
			1.把下载好的文件解压到目录中 2.以管理员身份运行cmd进入influxdb-1.7.6-1文件夹 3.生成influxdb数据的默认配置Config文件 4.修改Config配置信息 5.用配置好 ... 
- 第二类Stirling数初探 By cellur925
			上午noi.ac崩崩崩了,栽在组合数学上,虽说最后在辰哥&Chemist的指导下A掉了此题,也发现自己组合数学太弱了qwq. 在luogu上找题,结果找到了一个第二类斯特林数的题(还是双倍经验 ... 
- hdu 2189 悼念512汶川大地震遇难同胞——来生一起走 基础母函数
			#include <iostream> #include <algorithm> #include <cstring> using namespace std; ] ... 
- Eclipse - Maven项目Update Project后jdk版本变成1.5
			问题与分析 最近遇到个奇怪的问题,在Eclipse里对一个Maven项目进行Update Project(快捷键是 Alt+F5),原本jdk为1.8的项目忽然就变成了1.5,于是就报了一些错误. 我 ... 
- Selenium | 基础入门 | 利用Xpath寻找用户框
			以微信公众号登陆界面为例, 找到相对应的Xpath的方法, 核心代码: System.setProperty(“webdriver.chrome.driver”,“ C:\\Program Files ... 
- [在读]webkit技术内幕
			看了一点点,还不适合目前的我看,搁置. 
- Backbone.js入门教程第二版笔记(1)
			1.模块 集合 视图 和事件的一个综合例子 <!DOCTYPE html> <html> <head> <meta charset="UTF-8&q ... 
- PowerShell~发布你的mvc网站
			通过使用ps加上msbuild可以方便的编译你的.net应用程序,并且可以把它发布到你的磁盘上,部署非常方例! 我们在c盘添加一个hello网站,解决方案名是hello.sln,它的网站是hello. ... 
- [转]ASP.NET MVC Bootstrap极速开发框架
			本文转自:http://www.cnblogs.com/smartbooks/p/3814927.html 前言 每次新开发项目都要从头开始设计?有木有一个通用的快速开发框架?并且得是ASP.NET ... 
