我查询此查询会将相关图像返回到评论
return $comments = \DB::table('comments')->select('comments.comment','comments.user_name','comments.shop_name',
'comments.rating','comments.created_at','comments_images.images')->
join('comments_images','comments.id','=','comments_images.comment_id','left outer')
->where('comments.product_id', '=', "11644254552")->
orderBy(\DB::raw('-`images`'), 'desc')->get();
它会返回这样的结果
{
comment: "fdsfdsfsdffddsffds",
user_name: "ahmaf",
shop_name: "example.com",
rating: "4",
created_at: "2017-09-19 02:14:41",
images: https://example.com/kf/UT8.1.cX3XaXXagOFbX5.jpg
},
-{
comment: "fdsfdsfsdffddsffds",
user_name: "ahmaf",
shop_name: "example.com",
rating: "4",
created_at: "2017-09-19 02:14:41",
images: https://example.com/kf/UT81WzoX3BXXXagOFbXs.jpg
},
-{
comment: "sdfdsfdssfd",
user_name: "fdsfds",
shop_name: "example2.myshopify.com",
rating: "5",
created_at: "2017-09-19 02:12:25",
images: null
}
如您所见,前两行是重复的,我希望它们像这样返回
comment: "fdsfdsfsdffddsffds",
user_name: "ahmaf",
shop_name: "example.com",
rating: "4",
created_at: "2017-09-19 02:14:41",
images:{
0: https://example.com/kf/UT8.1.cX3XaXXagOFbX5.jpg
1: https://example.com/kf/UT81WzoX3BXXXagOFbXs.jpg
}
},
-{
comment: "sdfdsfdssfd",
user_name: "fdsfds",
shop_name: "example2.myshopify.com",
rating: "5",
created_at: "2017-09-19 02:12:25",
images: null
}
可能吗?返回它们以收集而不是为每个相关数据创建新行。
我试过它有效,但是我无法使用comments_images表中的orderBy图像,它只适用于评论中的列
$comments = Comment::where('product_id', '=', "11644215052")->with([
'images' => function ($query)
{
$query->orderBy('images', 'desc');
},
])->paginate(30);
答案 0 :(得分:0)
您正在寻找父/子关系结果。你不会用平行结果集得到它。看看使用laravel模型与子关系。
https://laravel.com/docs/5.5/eloquent-relationships#querying-relations