使用Eloquent时$post = Post::with('user', 'comments')->findOrFail($id) ;Laravel选择了三个请求
$post = Post::with('user', 'comments')->findOrFail($id)
select * from `posts` where `id` = ? limit 1 select * from `users` where `id` in (?) select * from `comments` where `comments`.`post_id` in (?)
是否有任何方法可以只发出一个请求?
sqougxex1#
我找到了解决办法
$post = Post::leftJoin('comments', 'posts.id', '=', 'comments.post_id') ->leftJoin('users', 'users.id', '=', 'comments.user_id') ->where('posts.id', $id) ->select('posts.*', 'users.name as commenter_name', 'comments.*') ->get();
1条答案
按热度按时间sqougxex1#
我找到了解决办法