I have query this query that returns the related images to the comment
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();
it returns results like this
{
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
}
as you see the first two rows are duplicated and I want them to be returned like this
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
}
is that possible ? to return them to gather instead of make new row for each related data.
I tried with it worked, but I couldn't use orderBy images from comments_images table, it only works with columns from comments
$comments = Comment::where('product_id', '=', "11644215052")->with([
'images' => function ($query)
{
$query->orderBy('images', 'desc');
},
])->paginate(30);