Laravel Eloquent Union query(Laravel Eloquent Union 查询)
问题描述
所以我有以下查询:
$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 查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Laravel Eloquent Union 查询
基础教程推荐
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- PHP 守护进程/worker 环境 2022-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- HTTP 与 FTP 上传 2021-01-01
- 使用 PDO 转义列名 2021-01-01