問題描述
所以我有以下查詢:
$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();
當(dāng)我先orderBy()"然后聯(lián)合時,不會發(fā)生排序.
No sorting is happening when I 'orderBy()' first and then union.
當(dāng)我單獨查詢 '$a' 或 '$b' 時,'orderBy()' 工作正常.
When I do query '$a' or '$b' individually the 'orderBy()' works fine.
當(dāng)我按照以下方式進(jìn)行時,orderBy()"會作為一個整體發(fā)生.
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()"分別適用于每個人,然后將結(jié)果合并回來?看起來它應(yīng)該可以工作.
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,我也會選擇你的作為答案,因為我認(rèn)為 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;
這篇關(guān)于Laravel Eloquent Union 查詢的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!