laravel eloquent 中有 unionall 的使用吗?

发布于 2025-01-12 17:13:10 字数 787 浏览 2 评论 0原文

我正在从数据库中的同一个表中提取两个不同的数据。我正在将这些数据与并集合并,但并未显示所有结果。所以我想使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

溺ぐ爱和你が 2025-01-19 17:13:10

您的查询使用集合中的 union不是查询生成器的 union 。这是因为在 union 之前有一个 get()。要获得与您需要的相当相似(但可能不相同)的结果,您可以执行以下操作:

$model = OrderDetail::with('product')->whereBetween('created_at', [Carbon::createFromDate($request['year_first'])->startOfYear(), Carbon::createFromDate($request['year_first'])->endOfYear()])
                ->unionAll(OrderDetail::with('product')->whereBetween('created_at', [Carbon::createFromDate($request['year_last'])->startOfYear(), Carbon::createFromDate($request['year_last'])->endOfYear()]))
     ->get()->groupBy(['service_id']);

这使得 unionunionAll 在查询的该部分中可用。旁注,您可能会遇到急切加载问题,我没有检查它是否会在联合查询中发生。

Your query uses union from the collection not the union of the query builder. This is because you have a get() before the union. To get a rather similar (but probably not identical) result to what you need you can do:

$model = OrderDetail::with('product')->whereBetween('created_at', [Carbon::createFromDate($request['year_first'])->startOfYear(), Carbon::createFromDate($request['year_first'])->endOfYear()])
                ->unionAll(OrderDetail::with('product')->whereBetween('created_at', [Carbon::createFromDate($request['year_last'])->startOfYear(), Carbon::createFromDate($request['year_last'])->endOfYear()]))
     ->get()->groupBy(['service_id']);

This makes union and unionAll 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文