我正在使用 Laravel 創建一個 API。
我創建了一個名為transaction. 我還創建了一個名為transactionresource.
我想將id列更改key為 API 中的列,并且我不希望 id 在 API 中顯示;相反,鍵應該顯示。
我的代碼
class TransactionResource extends JsonResource
{
public function toArray($request)
{
$array = parent::toArray($request);
// set the new key with data
$array['key'] = $array['id'];
// unset the old key
unset($array['id']);
return $array;
}
}
我收到此錯誤
{"success":false,"message":"Undefined array key \"id\""}
uj5u.com熱心網友回復:
您可以像這樣回傳自定義欄位陣列
public function toArray($request){
return [
'key' => $this->id,
...
];}
見檔案
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/422368.html
標籤:
下一篇:如何在來自資料庫的值中使用掩碼?
