我的控制器上的以下代码段有问题:
$opportunity_date = $oppr_arr->opportunity_date;
$locations_array_result = explode(",",$locations_array_result);
$usersCount = User::where('activated', '=', 1)
->where('group_id', '=', 1)
->where('availability_date', '<=', $opportunity_date)
->get();
foreach ($locations_array_result as $param) {
$usersCount = $usersCount->whereHas('desiredLocation', function ($q) use($param) {
$q->where('location_id', '=', $param );
});
}
$usersCount = $usersCount->count();
Run Code Online (Sandbox Code Playgroud)
当我运行它时,它给了我以下错误:
Call to undefined method Illuminate\Database\Eloquent\Collection::whereHas()
Run Code Online (Sandbox Code Playgroud)
我的人际关系是这样建立的:
用户模型
public function desiredLocation() {
return $this->belongsToMany('Location', 'user_desired_location');
}
Run Code Online (Sandbox Code Playgroud)
位置模型
class Location extends \Eloquent {
protected $table = 'locations';
protected $fillable = array('name');
public function country() {
return $this->belongsTo('Country');
}
}
Run Code Online (Sandbox Code Playgroud)
user_desired_location表的数据库结构是: …