In Laravel 9, use the API resource collection to disable the package data key of the outermost resource of a single interface
1. The initial response structure is as follows: as shown in Figure 1
{
"data": [
{
"id": 1,
"return_order_id": 61,
"return_order_item_id": 86
},
{
"id": 2,
"return_order_id": 61,
"return_order_item_id": 86
}
]
}
2. Since paging is not required, the front end wants to remove the package data key of the outermost resources, and set the “data” wrapper to null. Modify the resource collection class as follows
class ReturnOrderItemReceiptRecordResourceCollection extends ResourceCollection
{
/**
* 应该应用的「数据」包装器。
*
* @var string
*/
public static $wrap = null;
/**
* Transform the resource into an array.
*
* @param Request $request
* @return array
*/
public function toArray($request): array
{
return parent::toArray($request);
}
}
3. The result is as expected, and the interface responds to an array. as shown in Figure 2
[
{
"id": 1,
"return_order_id": 61,
"return_order_item_id": 86
},
{
"id": 2,
"return_order_id": 61,
"return_order_item_id": 86
}
]

