有没有办法把 x 带到哪里?

Is there a way to take x where?

我想做的是在 booltrue 的地方取 5,在 bool[ 的地方取 5 =24=] 是 false 并在 bool

上按 desc 排序
|# | Name  | Bool  |
|01| Test1 | true  |
|02| Test2 | false |
|03| Test3 | true  |
|04| Test4 | true  |
|xx| etc...

而不是这样做:

$boolTrue  = Test::where('bool', true)->get();
$boolFalse = Test::where('bool', false)->get();
return $boolFalse->merge($boolTrue)->sortByDesc('bool');

我只想向数据库发出一个请求,而不是 2 个,我想知道是否 像这样的事情是可能的:

// Obviously doesn't work
Test::where('bool', true)->take(5)->where('bool', false)->take(5)->orderBy('bool');

@Ron van der Heijden replied to me on my question. I should use unions.

我做了什么:

$boolTrue = Test::where('bool', true);
return Test::where('bool', false)->union($boolTrue)->orderByDesc('bool')->get();

这只会触发 1 个查询! :)

你可以像下面那样做。

$true = Test::where('bool', true); //Model instance
$false = Test::where('bool', false)->union($true); //Second model instance

$groupby = DB::query()->fromSub($false, 'nq')
->orderBy('bool')
->get();