0
的关系考虑一个模型雇员和模型项目Laravel /流明 - 限定与参数
的雇员表有一个属性类型可以分配下列值“1”, “2”, “3” 等
项目的hasMany雇员
public function programmers() {
return $this->hasMany('App\Employee')
->where('type', '1');
} // hasMany programmers
public function testers() {
return $this->hasMany('App\Employee')
->where('type', '2');
} // hasMany testers
public function managers() {
return $this->hasMany('App\Employee')
->where('type', '3');
} // hasMany managers
相反,这些关系的,我想只有一个:
public function employees($type_id) {
return $this->hasMany('App\Employee')
->where('type', $type_id);
} // hasMany employees
它的工作是这样的:
$app->get('/employee', function() {
$project = App\Employee::find(1);
return $project->employees("1");
});
不过,我收到以下异常:
ErrorException in Response.php line 402:
Object of class Illuminate\Database\Eloquent\Relations\HasMany could not be converted to string
谢谢,也为广泛的解释 –