3

I've created a materialized view: accounts_index that has a couple left joins, so I need to do a group by with the eloquent model. I also need to paginate the collection and am having trouble nailing the order of things. The materialized 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

The eloquent model can grab that table like so: AccountIndex::all();. I could paginate that: AccountIndex::paginate(); or do a groupBy: AccountIndex::all()->groupBy('account_uuid');, but lost on how to combine the two. Some attempts:

  • AccountIndex::all()->groupBy('account_uuid')->paginate(): Method Illuminate\Database\Eloquent\Collection::paginate does not exist.
  • AccountIndex::paginate()->groupBy('account_uuid');: returns the collection without paginating.
  • $accountsIndex = collect(\DB::table('accounts_index')->get()); $accountsIndex->groupBy('account_uuid')->paginate();: Same Method Illuminate\Support\Collection::paginate does not exist. exception.

The main problem I'm trying to solve is that these joins will be returning accounts that have multiple services (so multiple rows) and I need to then group by the account_uuid. Thanks in advance for any insights!

Matt Larsuma
  • 1,456
  • 4
  • 20
  • 52

2 Answers2

0

paginate method is related to Illuminate\Database\Query\Builder or Illuminate\Database\Eloquent\Builder classes, it is not exists in Collection class, the corresponding method for it in Collection class is forPage method, you can get more information from the documentation

Aboudeh87
  • 716
  • 5
  • 16
0

almost

AccountIndex::groupBy('account_uuid')->paginate()

paginate actually DOES something

groupBy() is just writing SQL just like ->where() ->join() etc that all prepares an SQL statement you can try by running ->toSql() returns an sql string.

get() all() paginate() all actually do something e.g. execute the SQL.

Toskan
  • 13,911
  • 14
  • 95
  • 185