問題描述
所以我有以下查詢:
$a = Model::where('code', '=', $code)
->where('col_a', '=' , 1)
->orderBy(DB::raw('FIELD(layout, "normal", "split", "flip", "double-faced", "") ASC, layout'))
$b = Model::where('code', '=', $code)
->where('col_b', '=' , 1)
->orderBy(DB::raw('FIELD(layout, "normal", "split", "flip", "double-faced", "") ASC, layout'))
$a->union($b)->get();
當我先orderBy()"然后聯合時,不會發生排序.
No sorting is happening when I 'orderBy()' first and then union.
當我單獨查詢 '$a' 或 '$b' 時,'orderBy()' 工作正常.
When I do query '$a' or '$b' individually the 'orderBy()' works fine.
當我按照以下方式進行時,orderBy()"會作為一個整體發生.
When I do it in the following way 'orderBy()' happens as a whole.
$a->union($b)
->orderBy(DB::raw('FIELD(layout, "normal", "split", "flip", "double-faced", "") ASC, layout'))
->get();
我怎樣才能使orderBy()"分別適用于每個人,然后將結果合并回來?看起來它應該可以工作.
How can I make it so the 'orderBy()' applies for each individually and then union the results back? It seems like it should work.
如果有人能提供一種方法來做到這一點,即使它是普通的 MySQL,我也會選擇你的作為答案,因為我認為 Eloquent 可能存在錯誤.
If anyone can provide a way to do this, even if it's normal MySQL, I will choose yours as the answer as I think there may be a bug with Eloquent.
推薦答案
嘗試以下操作:
$a = Model::where('code', '=', $code)
->where('col_a', '=' , 1);
$b = Model::where('code', '=', $code)->where('col_b', '=' , 1)
->union($a)
->get();
$result = $b;
這篇關于Laravel Eloquent Union 查詢的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!