我要在一个基类里面实例化所有的model 要根据集成的对象定义的model实例化
BaseRepository.php
<?php namespace AppRepository; class BaseRepository { //获取到model的对象 类似于再仓储里面直接 User::get() public function query() { $model = static::MODEL; //通过这个可以调用到子类定义的MODEL常量 return new $model(); } }
UserRepository.php
<?php namespace AppRepository; //仓储 用于连接service和model use AppModelUser; use IlluminateSupportFacadesDB; class UserRepository extends BaseRepository { const MODEL = User::class; public function getUserList() { $res = (new User())->first(); //new对象后查询 $res = User::first(); //直接静态调用查询 $query = $this->query(); //调用基类返回对象后查询 return $res; } }