php - Laravel Eloquent - 效率低下吗

标签 php mysql laravel eloquent

我正在考虑使用 Laravel 开发一个新站点。我正在阅读关于 http://vegibit.com/laravel-eloquent-orm-tutorial/ 的教程,关于使用 Eloquent 连接表和检索数据。在最后一个示例中,他们实际上是在尝试进行连接,但 Eloquent 仍然在执行两个查询而不是一个查询。 Eloquent 是效率低下还是这只是一个糟糕的例子?似乎这可以简单地通过单个左连接查询来完成。任何人都可以对此提供一些见解吗?有没有更好的方法来执行这个查询?具体例子是:

Route::get('/', function()
{
    $paintings = Painting::with('painter')->get();

    foreach($paintings as $painting){
        echo $painting->painter->username;
        echo ' painted the ';
        echo $painting->title;
        echo '<br>';
    }

});

以下查询的结果:

string ‘select * from paintings‘ (length=25)
string ‘select * from painters where painters.id in (?, ?, ?)’ (length=59)
Leonardo Da Vinci painted the Mona Lisa
Leonardo Da Vinci painted the Last Supper
Vincent Van Gogh painted the The Starry Night
Vincent Van Gogh painted the The Potato Eaters
Rembrandt painted the The Night Watch
Rembrandt painted the The Storm on the Sea of Galilee

最佳答案

Eloquent 对于快速构建某些东西非常有用,并且还具有查询缓存,它非常易于使用,但是对于需要随时间扩展的大型项目来说,它可能不是理想的选择,我的方法是使用带有契约(Contract)的存储库模式仍然可以使用 Eloquent 的强大功能,并拥有一个允许我在需要时轻松更改实现的系统。

关于php - Laravel Eloquent - 效率低下吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30606382/

相关文章:

mysql - 如何创建一个以域名作为主机名的MySQL服务器?

php - Laravel Eloquent 计数乘法

PHP/MYSQL更新多个表无法更新数据: Query was empty

php - 如何构造 PDOStatement 类?

php - 如何获取向服务器请求页面的客户端信息?

php - CodeIgniter phpMyAdmin - 插入名称中有空格的表

php - Laravel Eloquent 无法通过 $model->id 访问;

php - 我如何在 Laravel 授权中将数字传递给中间件 "can"

javascript - 如何从 Ajax Request 获取返回值并将其分配给一个元素?

php - 打开像页面一样的 href 到特定位置