This might sound like an obvious question but I just want to get some reassurance.
Using Laravel's eager loading functionality, from what I understand it will create two queries to return a whole list of related results (say if you're working with two tables). However, and correct me if I'm wrong, using a join statement will leave you with only one query, which creates one less round trip to the server's database (MySQL) and is a more efficient query.
I know that you can write join queries in Laravel, which is great, so the question is: am I incorrect to assume that when retrieving related data from two or more tables, should I not bother with eager loading and instead just write my own join statements?
****** Edit *******
Coming back to this one year later, I'd say in my personal opinion, just write the queries, raw, and write them well.
You are absolutely right about your understanding. If you write a
join
statement to join two or more tables usingjoin()
inLaravel
then it makes only one query where using anEloquent
model witheager loading
technique requires more than one query.Actually, the
eager loading
is a technique to load related models usingEloquent ORM
easily and it usesQuery Builder
behind the scene and lets you useEloquent Model Object
without making the query by your self and represents the data differently, UsingEloquent ORM
you are able to interact with model directly which represent objects in the database with additional features. Most importantly, it hides the complexity ofSQL
and allows you to do the database query in an OOP fashion usingPHP
code.But when you manually call
join
method which belongs toIlluminate\Database\Query\Builder
class then you are using theQuery Builder
directly which requires you write more code and requires more knowledge ofsql query
because it doesn't hide the query from you but helps you make queries more precisely, but you still make queries.Both are different things and they work differently. You may search on
Google
using the termORM vs Query Builder
.