小编RIT*_*NAG的帖子

调用 Laravel 中未定义的方法 Illuminate\Database\Eloquent\Collection::whereHas()

我的控制器上的以下代码段有问题:

$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表的数据库结构是: …

php eloquent laravel-4

2
推荐指数
1
解决办法
9842
查看次数

标签 统计

eloquent ×1

laravel-4 ×1

php ×1