I already get data like this Using laravel query builder
[0]
Category_id:1,
product:food
[1]
Category_id=1
product:fish
[2]
Category_id=1
product:vegetable
But I want this formate
category_id 1{
product
{ "Food","Fish","Vegetable"}
}
You can use pluck
:
$collection->pluck('product')->all();
The pluck method retrieves all of the values for a given key
Try this:
$collection->groupBy('category_id')
->map(function($item) {
return $item->pluck('product');
});