laravel eloquent 中有 unionall 的使用吗?
我正在从数据库中的同一个表中提取两个不同的数据。我正在将这些数据与并集合并,但并未显示所有结果。所以我想使用unionAll。我可以将它与 Eloquent 一起使用吗?
我的代码:
$model = OrderDetail::with('product')->whereBetween('created_at', [Carbon::createFromDate($request['year_first'])->startOfYear(), Carbon::createFromDate($request['year_first'])->endOfYear()])->get()->groupBy(['service_id'])
->union(OrderDetail::with('product')->whereBetween('created_at', [Carbon::createFromDate($request['year_last'])->startOfYear(), Carbon::createFromDate($request['year_last'])->endOfYear()])->get()->groupBy(['service_id']));
当我尝试使用 unionAll() 时,出现以下错误:
Method Illuminate\Database\Eloquent\Collection::unionAll does not exist.
UnionAll() 有不同的用途吗?
I am pulling two different data from the same table in the database.I'm merging this data with union but not all results are shown. So I want to use unionAll. Can I use it with Eloquent?
My code:
$model = OrderDetail::with('product')->whereBetween('created_at', [Carbon::createFromDate($request['year_first'])->startOfYear(), Carbon::createFromDate($request['year_first'])->endOfYear()])->get()->groupBy(['service_id'])
->union(OrderDetail::with('product')->whereBetween('created_at', [Carbon::createFromDate($request['year_last'])->startOfYear(), Carbon::createFromDate($request['year_last'])->endOfYear()])->get()->groupBy(['service_id']));
When I try with unionAll() I get the following error:
Method Illuminate\Database\Eloquent\Collection::unionAll does not exist.
Does unionAll() have different uses?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的查询使用集合中的
union
不是查询生成器的union
。这是因为在union
之前有一个get()
。要获得与您需要的相当相似(但可能不相同)的结果,您可以执行以下操作:这使得
union
和unionAll
在查询的该部分中可用。旁注,您可能会遇到急切加载问题,我没有检查它是否会在联合查询中发生。Your query uses
union
from the collection not theunion
of the query builder. This is because you have aget()
before theunion
. To get a rather similar (but probably not identical) result to what you need you can do:This makes
union
andunionAll
usable in that part of the query. Sidenote you might have issues with the eager loading, I've not checked if it can happen in a union query.