Skip to main content

Eager Loading with load() and $with

Premium
2:14

The Full Lesson is Only for Premium Members

Want to access all of our courses? (36 h 00 min)

You also get:

61 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

MGeorgi avatar

This is really interesting. Would using protected $with=['relation'] be a good choice for 1:1 relations?

I remember a database where I had a table "material_movements" for internal and external movements. If a movement is external (delivery / shipmen), it needs some extra field and those are store in an extra table called material_movements_external. The tables are linked by a 1:1 relationship and I can't really think about a case where I would not want to load the extarnal data (if it exists).

In this case having something like protected $with=['externalMovement'] would be a good usecase right? If anyone has an opinion on that I'd be interested to hear from you 🙂

Povilas Korop avatar

It's hard to say 100% until you actually try it on a real project and ensure that you actually need it EVERYWHERE with all the queries.

Personally, I stopped using $with a while ago, "just in case", and became a fan of specifying the relationship needed only when I need it.

Because maybe in the future other developers would use the model differently and would wonder why that additional query is coming automatically.