使用 Lumen 8,我有:
- 型號 : 商店
- 控制器:商店控制器
- 存盤庫:StoreRepository
我可以Store毫無問題地得到我的并得到下面的json:
{
"id_store": 1,
"name": "Abcde",
"email": "",
"phone": "0123456789"
}
我想獲得Store. 在我的模型中,我有一個陣列(用于測驗),我可以這樣顯示它:
return response()->json(['item' => $store, 'time' => $store->getTime()], 200);
并得到這個結果:
{
"item": {
"id_store": 1,
"name": "Abcde",
"email": "",
"phone": "0123456789"
},
"time": {
"1": "January",
"2": "February",
"3": "March"
}
}
問題是我現在有 2 個部分。我怎樣才能在我的存盤值中有這個陣列?如下所示:
{
"id_store": 1,
"name": "Abcde",
"email": "",
"phone": "0123456789"
"time": {
"1": "January",
"2": "February",
"3": "March"
}
}
uj5u.com熱心網友回復:
讀錯了,在 Laravel 中,你可以使用 Resources API Resources,我猜 Lumen 沒有,更多檔案在這里:https ://laravel.com/docs/8.x/eloquent-resources
在你的資源中,只有這樣的東西:
public function toArray($request)
{
return [
'id_store' => $this->id_store,
'name' => $this->name,
'email' => $this->email,
'phone' => $this->phone,
'time' => $this->->getTime(),
];
}
或者只是回傳這樣的東西:
$store->time = $store->getTime();
return response()->json($store, 404);
為什么是 404 順便說一句?
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/414195.html
標籤:
下一篇:Foreach不回傳所有值
