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
謝謝,也爲廣泛的解釋 –