0

So i have this table

|id|    price    |  start_time |
--------------------------------
|1 |    45000.00 |  04:45:00   |
|1 |    45000.00 |  07:00:00   |
|2 |    40000.00 |  01:20:00   |

I want to make a collection that return json like this

{
   id: 1
   price: 45000.00
   start_time: [
        04:45:00, 07:00:00
    ]
},
{
   id: 2
   price: 40000.00
   start_time: [
        01:20:00
    ]
}

I spent a lot of time to figure it out but I can't do it

Please help me to figure it out,thanks in advance

2
  • u want in in hole application or in single route .? Commented Sep 12, 2020 at 9:42
  • in a single route Commented Sep 12, 2020 at 9:43

1 Answer 1

1

use map()

 $laravelCollcetion->map(function($row){
       return $row['start_time'] = [$row['start_time']]
  })

return $laravelCollcetion;

ref link https://laravel.com/docs/7.x/collections#extending-collections


for your db instance u can do like this

 $data = \DB::table('schedules')
        ->select(\DB::raw('id, price,GROUP_CONCAT(start_time) as start_time'))
        ->groupBy('id')
        ->get();

return $data;
Sign up to request clarification or add additional context in comments.

1 Comment

and now iam struggling at my queries , my queries right now is DB::table('schedules')->select('id','price','start')->groupBy('id', 'price')->get(); but i turns out an error . do you have any idea what queies should i use. thanks in advance

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.