Спасибо я так и поступил, вот что получилось:
<?php
namespace App\Http\Resources\Infobject;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
/**
* @property mixed id
* @property mixed type
* @property mixed name
*/
class InfobjectResource extends JsonResource
{
protected $wayLineColor = '';
public function setWayLineColor($wayLineColor) {
$this->wayLineColor = $wayLineColor;
return $this;
}
/**
* Transform the resource into an array.
* Массив объектов инфраструктуры для массива линейных объектов
* @param $request
* @return array
*/
public function toArray($request)
{
return [
'id' => $this->id,
'label' => $this->type . ' ' . $this->name,
'color' => $this->wayLineColor,
];
}
public static function collection($resource)
{
return new InfobjectResourceCollection($resource);
}
}
<?php
namespace App\Http\Resources\Infobject;
use Illuminate\Http\Resources\Json\ResourceCollection;
class InfobjectResourceCollection extends ResourceCollection
{
protected $wayLineColor = '';
public function setWayLineColor($wayLineColor) {
$this->wayLineColor = $wayLineColor;
return $this;
}
/**
* Transform the resource collection into an array.
*
* @param $request
* @return array
*/
public function toArray($request)
{
$this->collection->each->setWayLineColor($this->wayLineColor);
return parent::toArray($request);
}
}
Сам проброс:
<?php
namespace App\Http\Resources\Infobject;
use Illuminate\Http\Resources\Json\JsonResource;
/**
* @property mixed id
* @property mixed name
* @property mixed code
* @property mixed color
*/
class InfobjectWithWayLineResource extends JsonResource
{
/**
* Transform the resource into an array.
* Внешний массив линейные объекты
* @param $request
* @return array
*/
public function toArray($request)
{
return [
'id' => $this->id,
'label' => $this->name . ' (' . $this->code . ')',
'color' => $this->color,
'children' => InfobjectResource::collection($this->whenLoaded('infobjects'))
->setWayLineColor($this->color)
];
}
}