Laravel を使用して API を作成しています。
というテーブルを作成しましたtransaction
。というリソースも作成しましたtransactionresource
。
id
列を APIの列に変更したいのですがkey
、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\""}