【发布时间】:2019-11-07 18:08:23
【问题描述】:
从这里:https://laravel-news.com/eloquent-eager-loading
运行这一行:App\Post::with('author.profile')->get(); 将执行 3 个查询:
[2017-08-04 07:27:27] local.INFO: select * from `posts`
[2017-08-04 07:27:27] local.INFO: select * from `authors` where `authors`.`id` in (?, ?, ?, ?, ?) [1,2,3,4,5]
[2017-08-04 07:27:27] local.INFO: select * from `profiles` where `profiles`.`author_id` in (?, ?, ?, ?, ?) [1,2,3,4,5]
怎么会? 有没有一种简洁的方法可以在一个查询中做到这一点?
我希望得到的查询看起来像:
SELECT *
FROM posts, authors, profiles
WHERE posts.author_id = author.id
AND author.profile_id = profiles.id
【问题讨论】:
-
建议查询的一大缺点:同名的列会相互覆盖。在您的情况下,结果将有一个
id列,其值将来自profiles(最后一个表)。
标签: laravel eloquent laravel-5.8