【发布时间】:2016-11-01 19:41:34
【问题描述】:
考虑一个模型Employee和一个模型Project
employees 表 有一个属性type,可以分配以下值“1”、“2”、“3”等。
项目有很多员工
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
【问题讨论】: