例如,我有两个查询:
1:
$q = SomeContent::select('somecontent_id')
->where('slug', Request::segment(2))
->where('something', $something)
->first();
2
$req = SomeContent::select('slug')
->where('something', $anothersomething)
->where('somecontent_id', $q->somecontent_id)
->first();
如果可能的话,我该如何在laravel的查询生成器中将它们合并为一个查询?我找不到太多关于在where语句中使用select语句的信息。
您可以将它们联合在一起,类似
// The query builder also provides a quick way to "union" two queries together:
$q = SomeContent::select('somecontent_id')
->where('slug', Request::segment(2))
->where('something', $something);
$req = SomeContent::select('slug')
->where('something', $anothersomething)
->where('somecontent_id', $q->somecontent_id)
->union($q)->get();
您也可以使用orWhere函数
$q = SomeContent::select('somecontent_id')
->where('slug', Request::segment(2))
->where('something', $something)
->orWhere(function($query)
{
$query->where('something', $anothersomething)
->where('somecontent_id', $q->somecontent_id)
->union($q)->get();
})
->get();