如何打破雄辩的查询:: with()

时间:2015-10-19 07:32:27

标签: mysql laravel laravel-4

使用像这样的雄辩单表查询

$query = Lists::query();

我们可以保存一个临时变量,但是当我们使用像这样的多个表时

Lists::with(['tags','keyproduct','productcatalog']);

为什么我们不能将它保存在上面的变量中?我想要存储在这样的临时变量中的原因是因为我想在查询中使用条件语句,如

if($something==true)
   $query->where('type'=>'Paid') 

知道如何使用:: with()?

来分解查询

1 个答案:

答案 0 :(得分:0)

你可以在whrehas中给出条件

$posts = List::whereHas('comments', function($q)
{
if($something==true){
$q->where('content', 'like', 'foo%');
}
})->get();

有关详细信息http://laravel.com/docs/5.0/eloquent#querying-relations