【问题标题】:how to using group by for relationships in laravel?如何在 laravel 中使用 group by 关系?
【发布时间】:2022-01-25 18:59:59
【问题描述】:

如何加入变形关系以及如何在另一个组内创建组

我还需要状态计数

TicketHistory::groupBy('from_id')
            ->with(['statuses' => function ($query) {
                return $query->groupBy('name');
            }])->get();


TicketHistory::query()
            ->groupBy('from_id')
            ->join('model_statuses', 'ticket_history.id', '=', 'model_statuses.model_id')
            ->selectRaw('count(model_statuses.id) as count, status_count')
            ->groupBy('status_count')
            ->get();
  • groupby from_id
  • 然后在 from_id groupby 状态中
  • 并且有 2 个具有变形关系的表
  • 并且有 2 个具有变形关系的表

【问题讨论】:

  • 欢迎来到 SO。请说明您在寻找什么以及您的解决方案有什么问题。
  • 它只是按 from_id 而不是状态分组

标签: php laravel database polymorphism backend


【解决方案1】:
 $ticketHistories = $ticketHistoryQuery->get()
            ->groupBy(
                [
                    'from_id',
                    function ($item) {
                        return $item->status;
                    }
                ],
                $preserveKeys = true
            );

这个可以用但是太慢了

【讨论】:

  • 您不应在答案中发布问题。删除此答案并将其作为更新添加到您的问题中。
猜你喜欢
  • 2017-04-04
  • 1970-01-01
  • 2021-04-07
  • 2021-06-03
  • 2019-03-31
  • 1970-01-01
  • 2023-04-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多