<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\ResourceCollection;
class LanguageCollection extends ResourceCollection
{
public $collects = LanguageResource::class;
/**
* TagCollection constructor.
* @param $resource
*/
public function __construct($resource)
{
parent::__construct($resource);
}
/**
* Transform the resource collection into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'languages' => parent::toArray($request)
];
}
}
这将给出如下响应结构:
但我想将所有语言放在另一个名为“result”的包装器中,因此响应结构将如下所示:
我不想以这种方式对其进行硬编码,因为这需要我们更改每个资源集合。
public function toArray($request)
{
return [
'result' => ['languages' => parent::toArray($request)]
];
}
实现这种响应结构的正确方法是什么?