我正在使用 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\""}