2013-11-20 23 views
1

我有这方面的工作查询,它​​可以获取所有项目,他们的国家,并为国家佞(来自其他表?)

$projects = Project::with('country.continent')->get(); 

各大洲现在,我试图做一个在哪里,只得到一个特定的大陆,结果用下面的查询

$projects = Project::where('continent.name', $sub)->with('country.continent')->get(); 

但这是失败,它寻找的项目表“continent.name”,但它在嵌套表

有任何想法吗?谢谢!

回答

2

Laravel docs

贪婪加载约束

有时你不妨渴望负载的关系,而且还指定 的渴望负载的条件。这里有一个例子:

$users = User::with(array('posts' => function($query) 
{ 
    $query->where('title', 'like', '%first%'); 
}))->get(); 

在这个例子中,我们渴望加载用户的帖子,但前提是 帖子标题栏包含单词“第一”。

所以[警告:未经测试的代码]

$projects = Project::with(array('country.continent') => function($query) use ($sub){ 
    $query->where('name',$sub);}))->get(); 
} 

可能做到这一点。