我想要有两个不同的API版本,并想要有两个不同的UserCollection输出。我已经尝试过了:
class Branch extends JsonResource
{
private $usage;
public function __construct($resource, $usage = null) {
$this->usage = $usage;
parent::__construct($resource);
}
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'id' => (string) $this->id,
'code' => $this->code,
'name' => $this->name,
$this->mergeWhen($this->usage == 'select-branch', [
'address' => $this->address,
'phone' => $this->phone,
]),
$this->mergeWhen($this->usage == 'api', [
'bank_id' => $this->bank_id,
]),
];
}
}
,并且UserCollection中没有更改。
但是第一个元素同时具有合并功能,其余元素没有任何合并功能。
当我从集合中调用资源时, dd($this->usage)
在第一个资源中为0,在第二个资源中为1,依此类推。