Laravel eager loading vs explicit join

前端 未结 1 592
醉话见心
醉话见心 2020-12-15 06:21

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 creat

相关标签:
1条回答
  • 2020-12-15 06:47

    You are absolutely right about your understanding. If you write a join statement to join two or more tables using join() in Laravel then it makes only one query where using an Eloquent model with eager loading technique requires more than one query.

    should I not bother with eager loading and instead just write my own join statements

    Actually, the eager loading is a technique to load related models using Eloquent ORM easily and it uses Query Builder behind the scene and lets you use Eloquent Model Object without making the query by your self and represents the data differently, Using Eloquent ORM you are able to interact with model directly which represent objects in the database with additional features. Most importantly, it hides the complexity of SQL and allows you to do the database query in an OOP fashion using PHP code.

    But when you manually call join method which belongs to Illuminate\Database\Query\Builder class then you are using the Query Builder directly which requires you write more code and requires more knowledge of sql 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 term ORM vs Query Builder.

    0 讨论(0)
提交回复
热议问题