我要在一個基類裏面實例化全部的model 要根據集成的對象定義的model實例化php
BaseRepository.php
<?php namespace App\Repository; class BaseRepository { //獲取到model的對象 相似於再倉儲裏面直接 User::get() public function query() { $model = static::MODEL; //經過這個能夠調用到子類定義的MODEL常量 return new $model(); } }
UserRepository.php
<?php namespace App\Repository; //倉儲 用於鏈接service和model use App\Model\User; use Illuminate\Support\Facades\DB; class UserRepository extends BaseRepository { const MODEL = User::class; public function getUserList() { $res = (new User())->first(); //new對象後查詢 $res = User::first(); //直接靜態調用查詢 $query = $this->query(); //調用基類返回對象後查詢 return $res; } }