这听起来可能是一个显而易见的问题,但我只是想得到一些保证.

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 *******

一年后再回到这一点,我个人认为,只要把问题写出来,原封不动,把它们写好.

*编辑2*

好了,六年后,我一直在为此得分.

Whether I was unclear from the beginning or not, contrary to what I've said above, Eloquent at this point writes great queries. Use Eloquent - even if there's a slight query inefficiency, it allows you to write very clear, maintainable code which at this point in my career I would argue is more important in most cases. Only write raw queries and optimize in cases where performance enhancements are critical and you can measure the impact.

推荐答案

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.

两者都是不同的东西,工作方式也不同.你可以用ORM vs Query Builder搜索Google.

Laravel相关问答推荐

Nginx上带有SSL的Laravel混响

spatie 包 laravel-tags 在迁移中没有 down() 函数是有原因的吗

无法找到用户sail:passwd 文件中没有匹配的条目

Laravel:在失败时处理 findOrFail()

如何在 NetBeans 中添加带有点 (blade.php) 的自定义文件扩展名?

Laravel 5 - 仅在特定页面/控制器(页面特定assets资源)上添加样式表

Laravel 5.1 是否与 PHP 7 兼容

如何在 Laravel 中创建类别的嵌套列表?

如何对模型执行删除操作?

如何从 Windows 命令提示符在 Laravel 中运行 PHPUnit

如何在 laravel 中解码 Json 对象并在 laravel 中应用 foreach 循环

laravel 4:更新行时如何从列的当前值中减一?

扩展 Eloquent 的类的构造函数

无法打开laravel.log:无法打开流

如何在 Laravel 中使用补丁请求?

Laravel 5 事件处理程序未触发

使用 Nginx 设置 Laravel

php artisan migrate - SQLSTATE [HY000] [1045] 用户'laravel'@'localhost'的访问被拒绝

Laravel 数据迁移

如何从 PHPUnit 测试设置运行 Laravel 数据库 seeder 机?