所以我有這張桌子|id| price | start_time |--------------------------------|1 | 45000.00 | 04:45:00 ||1 | 45000.00 | 07:00:00 ||2 | 40000.00 | 01:20:00 |我想創(chuàng)建一個像這樣返回 json 的集合{ id: 1 price: 45000.00 start_time: [ 04:45:00, 07:00:00 ]},{ id: 2 price: 40000.00 start_time: [ 01:20:00 ]}我花了很多時間去弄清楚但我做不到請幫我弄清楚,提前謝謝
1 回答

當(dāng)年話下
TA貢獻1890條經(jīng)驗 獲得超9個贊
使用map()
$laravelCollcetion->map(function($row){
return $row['start_time'] = [$row['start_time']]
})
return $laravelCollcetion;
參考鏈接https://laravel.com/docs/7.x/collections#extending-collections
對于您的數(shù)據(jù)庫實例,您可以這樣做
$data = \DB::table('schedules')
->select(\DB::raw('id, price,GROUP_CONCAT(start_time) as start_time'))
->groupBy('id')
->get();
return $data;
- 1 回答
- 0 關(guān)注
- 124 瀏覽
添加回答
舉報
0/150
提交
取消