Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

Eager Loading with load() and $with

Premium
2:14

The Full Lesson is Only for Premium Members

Want to access all of our courses? (29 h 14 min)

You also get:

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

Already a member? Login here

Comments & Discussion

M
MGeorgi ✓ Link copied!

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 🙂

PK
Povilas Korop ✓ Link copied!

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.

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.