gpt4 book ai didi

Laravel/Eloquent 分页和 groupBy 用于 postgres 物化 View 模型

转载 作者:行者123 更新时间:2023-11-29 14:02:40 24 4
gpt4 key购买 nike

我创建了一个物化 View :accounts_index,它有几个左连接,所以我需要用 Eloquent 模型做一个分组。我还需要对集合进行分页,并且无法确定事物的顺序。物化 View :

CREATE materialized VIEW accounts_index
AS SELECT a.account_uuid,
a.account_no,
a.person_owner_uuid,
a.company_owner_uuid,
a.account_group_uuid,
a.account_scope_uuid,
a.created_at,
a.deleted_at,
s.service_uuid,
s.status,
st.service_type
FROM accounts a
LEFT JOIN services s
ON a.account_uuid = s.account_uuid
LEFT JOIN service_types st
ON s.service_type_uuid = st.service_type_uuid

Eloquent 模型可以像这样获取该表:AccountIndex::all();。我可以对其进行分页:AccountIndex::paginate(); 或进行 groupBy:AccountIndex::all()->groupBy('account_uuid');,但在如何将两者结合起来。一些尝试:

  • AccountIndex::all()->groupBy('account_uuid')->paginate():方法 Illuminate\Database\Eloquent\Collection::paginate 不存在。
  • AccountIndex::paginate()->groupBy('account_uuid');:不分页返回集合。
  • $accountsIndex = collect(\DB::table('accounts_index')->get());
    $accountsIndex->​​groupBy('account_uuid')->paginate();
    :相同的方法 Illuminate\Support\Collection::paginate 不存在。异常。

我试图解决的主要问题是这些连接将返回具有多个服务(因此多行)的帐户,然后我需要按 account_uuid 进行分组。提前感谢您的任何见解!

最佳答案

paginate 方法与 Illuminate\Database\Query\BuilderIlluminate\Database\Eloquent\Builder 类相关,它不存在在 Collection 类中,它在Collection类中对应的方法是forPage方法,你可以从documentation中获得更多信息。

关于Laravel/Eloquent 分页和 groupBy 用于 postgres 物化 View 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56675881/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com