問題描述
所以我有以下查詢:
$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)合時(shí),不會(huì)發(fā)生排序.
No sorting is happening when I 'orderBy()' first and then union.
當(dāng)我單獨(dú)查詢 '$a' 或 '$b' 時(shí),'orderBy()' 工作正常.
When I do query '$a' or '$b' individually the 'orderBy()' works fine.
當(dāng)我按照以下方式進(jìn)行時(shí),orderBy()"會(huì)作為一個(gè)整體發(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()"分別適用于每個(gè)人,然后將結(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.
如果有人能提供一種方法來做到這一點(diǎn),即使它是普通的 MySQL,我也會(huì)選擇你的作為答案,因?yàn)槲艺J(rèn)為 Eloquent 可能存在錯(cuò)誤.
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 查詢的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!