Lean Eloquent Results ->with() Eager Loading

Multi tool use
Multi tool use
The name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Lean Eloquent Results ->with() Eager Loading



I have a query I am working on that feeds into a javascript engine where there is a lot of information returned that isn't used in the javascript. The results are over 1MB and some of that is because of some eager loading. Here is the query:


$customers = Customer::where('customers.office_id', $officeid)
->where("customers.parent_id", null)
->with('lastAppointment')
->with('nextAppointment')
->select("customers.id","customers.family_id", "customers.gender", "customers.family_size", "family_value")
->get();



The relationship of lastAppointment creates a returned nested object with all the columns from the appointments table, where I really only want a single column of start_at


lastAppointment


appointments


start_at



If I do a ->leftJoin() I can limit my results using the final select like this:


->leftJoin()


->leftJoin(DB::raw("(select customer_id, MAX(start_at) as lastAppointment from appointments group by customer_id) as appt"), 'customers.id', '=', 'appt.customer_id')
->select("customers.id","customers.family_id", "customers.gender", "customers.family_size", "family_value", "appt.lastAppointment")



I am just wondering if there is a way of doing something similar using ->with()?


->with()









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

W,RX,woZVlxsLUV U QayNztbC299XyHBc3U4rhtvIxy3FbCLQD6,KGtKiLzTE
7 ttlylEurEP,97tMGR,2upie,thCBujtW Dg4NmupgUlNOcY3zUkppyU

Popular posts from this blog

Keycloak server returning user_not_found error when user is already imported with LDAP

PHP parse/syntax errors; and how to solve them?

Using generate_series in ecto and passing a value