gpt4 book ai didi

php - Laravel Eloquent - 效率低下吗

转载 作者:行者123 更新时间:2023-11-29 05:17:37 25 4
gpt4 key购买 nike

我正在考虑使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com