Php Can';查询中使用连接时的访问相关模型

Php Can';查询中使用连接时的访问相关模型,php,laravel,Php,Laravel,我正在尝试按状态筛选我的代理模型。状态列位于一个单独的模型上,即用户。在我的存储库中,我是这样做的: public function blocked($howMany = 10) { return $this->model->join('users', 'users.userable_id', '=', 'agents.id') ->where(function($query) { $que

我正在尝试按状态筛选我的代理模型。状态列位于一个单独的模型上,即用户。在我的存储库中,我是这样做的:

public function blocked($howMany = 10)
{
    return $this->model->join('users', 'users.userable_id', '=', 'agents.id')
            ->where(function($query)
            {
                $query->where('users.status', config('user.status.blocked'));
            })
            ->paginate($howMany);
}
其中,$this->model的值是注入构造函数中的代理模型实例。然而,现在,这段代码在控制器的循环中起作用:

public function blocked()
{
    $agents = $this->agents->blocked();

    foreach ($agents as $agent)
    {
        dd($agent->user);
    }

    return view('agents.admin', compact('agents'));
}

user的值为空。这样做的最佳方式是什么?

通常,你们的关系应该如下所示:

public function blocked($howMany = 10)
{
    return $this->hasMany('users', 'userable_id', 'id')
                ->where('status','=',config('user.status.blocked'))
                ->paginate($howMany);
}
资料来源: