一、从头开始创建

1、执行以下artisan:

php artisan make:entity Student

l5-repository基本使用--结合使用artisan

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

l5-repository基本使用--结合使用artisan

2、修改model:Student.php

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

l5-repository基本使用--结合使用artisan

3、StudentRepository.php增加方法:

public function getInfoById($id, $sel);

l5-repository基本使用--结合使用artisan

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();
    }

l5-repository基本使用--结合使用artisan

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']);
    }
}

l5-repository基本使用--结合使用artisan

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(1);
        dd($data);
    }
}

l5-repository基本使用--结合使用artisan

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

Route::get('/student/test', '[email protected]');

l5-repository基本使用--结合使用artisan

l5-repository基本使用--结合使用artisan

8、注册RepositoryServiceProvider:

l5-repository基本使用--结合使用artisan

 

其他随笔l5-repository基本使用

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-08-20
  • 2022-12-23
  • 2021-11-30
  • 2021-11-11
  • 2021-05-19
  • 2022-01-07
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-09-18
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案