【发布时间】:2021-10-21 02:33:08
【问题描述】:
我要做的是在 bool 为 true 的地方取 5,在 bool 为 false 的地方取 5 em> 并在 bool
上对它们进行排序|# | 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');
我只想向数据库发出 1 个请求而不是 2 个请求,并且想知道是否 这样的事情是可能的:
// Obviously doesn't work
Test::where('bool', true)->take(5)->where('bool', false)->take(5)->orderBy('bool');
【问题讨论】:
-
你的意思是unions?
-
@RonvanderHeijden 是的!这将回答我的问题!谢谢你!
标签: php mysql laravel eloquent laravel-6