【发布时间】:2018-03-03 00:44:37
【问题描述】:
我已查询此查询,该查询将相关图像返回到评论
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
}
这可能吗?将它们返回以收集而不是为每个相关数据创建新行。
我尝试使用它,但我无法使用 cmets_images 表中的 orderBy 图像,它仅适用于 cmets 中的列
$comments = Comment::where('product_id', '=', "11644215052")->with([
'images' => function ($query)
{
$query->orderBy('images', 'desc');
},
])->paginate(30);
【问题讨论】:
-
尝试添加
->groupBy('comments.id')->get();!! -
groupBy 不起作用,如果我这样做,我将不得不在其中添加所有列,否则它将返回错误,因此它不会返回所有记录。我只想将重复的行组合在一起
标签: php mysql sql laravel laravel-5