我有以下存储库:
<?php
namespace App\Repositories;
use App\Employee;
class EmployeeRepository
{
protected $employee;
...
public function getAllManagersList(Employee $employee)
{
$managers = $employee->getAllManagers();
$result = Array();
foreach ($managers as $manager)
{
$result[$manager->id] = $manager->first_name . ' ' . $manager->last_name;
}
return result;
}
}
正在调用模型:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use DB;
class Employee extends Model {
protected $table = 'employees';
public $timestamps = true;
...
public function getAllManagers()
{
$managers = DB::table('employees')->select(['id','first_name','last_name'])->orderBy('last_name')->where('is_manager','=','1')->get();
return $managers;
}
}
我的控制器正在调用它:
public function create(EmployeeRepository $employeeRepository)
{
$manager_list = $employeeRepository->getAllManagersList();
//\Debugbar::info($manager_list);
return view('employees/create')->with('manager_list',$manager_list);
}
但是当我运行这个时,我得到以下内容:
Type error: Argument 1 passed to App\Repositories\EmployeeRepository::getAllManagersList() must be an instance of App\Employee, none given, called in C:\xampp\htdocs\people\app\Http\Controllers\EmployeeController.php on line 31
我试图以一种简洁的方式得到它,以便我可以让我的模型只做db请求和我的存储库来整理它,以便它在我的页面中显示得很好。这就是我尝试将其从控制器调用到存储库到模型的原因。
答案 0 :(得分:0)
You are calling $employeeRepository->getAllManagersList();
.
But the function is defined as :
public function getAllManagersList(Employee $employee)
.