Courses

Better Eloquent Performance

Eager Loading with load() and $with

Previous: N+1 Query: "Deeper" Examples - Packages and Count
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 🙂

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.

avatar
You can use Markdown
avatar
You can use Markdown