zoukankan      html  css  js  c++  java
  • 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 IlluminateSupportFacadesDB;
        public function getInfoById($id, $sel)
        {
            return $this->model
                        ->select(DB::raw(implode(',', $sel)))
                        ->where('id', $id)
                        ->first();
        }

    5、新增StudentService.php

    <?php
    
    namespace AppServices;
    
    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 AppHttpControllers;
    
    use AppRepositoriesStudentRepository;
    use AppServicesStudentService;
    
    
    /**
     * Class StudentsController.
     *
     * @package namespace AppHttpControllers;
     */
    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(1);
            dd($data);
        }
    }

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

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

    8、注册RepositoryServiceProvider:

    其他随笔l5-repository基本使用

  • 相关阅读:
    20145220韩旭飞《网络对抗》第一周学习
    学号20145220假期复习
    最简单易懂的Idea搭建SSM项目过程和配置(文末有demo哦)
    Navicate快捷键
    String.format()方法
    tomcat点击startup.bat出现闪退
    Shiro使用总结
    layer弹出层icon图标记录
    传统javabean与spring中的bean的区别【转】
    在Spring + SpringMVC + mybatis架构的项目基础上集成Shiro
  • 原文地址:https://www.cnblogs.com/zhengchuzhou/p/9927962.html
Copyright © 2011-2022 走看看