Laravel有几个渴望加载限制

时间:2014-12-04 06:18:31

标签: php mysql laravel-4

我遇到有关带有约束的Eager Loading的问题。我有一个返回所有项目的方法。

public function findProjectById($id, $user_id)
{
    $project = Project::with([
            'tasklists' => function($query) {
                $query->with(['tasks' => function($q) {
                    $q->with(['subtasks', 'comments' => function($com) {
                        $com->with('user');
                    }]);
                }])
                ->with('project');
            }
        ])->findOrFail($id);

    return $project;
}

这很好用。但我想只返回任务属于当前用户的那些项目。用户(user_id)的任务表中有一个字段。为此我在任务中使用了where查询。

$query->with(['tasks' => function($q) {
    $q->where('user_id', '=', $user_id);
    $q->with(['subtasks', 'comments' => function($com) {
        $com->with('user');
    }]);
}])

但它抛出错误异常“Undefined variable user_id”

"type":"ErrorException","message":"Undefined variable: user_id"

任何人都可以指出似乎是什么问题。感谢。

0 个答案:

没有答案