目前我正在構建一個帶有 flutter 的應用程序,但我的 Laravel api 控制器沒有返回適當的多維數組。我想要 { [0]=> 數組,[1]=> 數組等}。如果有人知道解決此問題的方法,我們將不勝感激public function index() { $payments = Payment::all(); return $payments; }它返回這個:[ { "id": 1, "description": "test", "price": "9.00", "currency": "EUR", "status": "open", "deleted_at": null, "created_at": "2020-08-10T09:50:52.000000Z", "updated_at": "2020-08-10T09:50:52.000000Z" }, { "id": 2, "description": "test", "price": "3.00", "currency": "USD", "status": "open", "deleted_at": null, "created_at": "2020-08-13T19:06:23.000000Z", "updated_at": "2020-08-13T19:06:23.000000Z" }]我嘗試了很多,這讓我最接近,但后來我得到了 1:{ARRAY}我嘗試過的代碼:public function index() { $data = []; $payments = Payment::all(); $id=1; foreach($payments as $payment){ array_push($data,[$id=>$payment]); $id++; } return $data; }結果:[ { "1": { "id": 1, "description": "test", "price": "9.00", "currency": "EUR", "status": "open", "deleted_at": null, "created_at": "2020-08-10T09:50:52.000000Z", "updated_at": "2020-08-10T09:50:52.000000Z" } }, { "2": { "id": 2, "description": "test", "price": "3.00", "currency": "USD", "status": "open", "deleted_at": null, "created_at": "2020-08-13T19:06:23.000000Z", "updated_at": "2020-08-13T19:06:23.000000Z" } } ]我想擁有:{1:{ "message":"test", "price":"9" "currency":"EUR" },2:{ "message":"test", "price":"9" "currency":"EUR"}
- 2 回答
- 0 關注
- 122 瀏覽
添加回答
舉報
0/150
提交
取消