Answer the question
In order to leave comments, you need to log in
How to forward a property in a child to a collection of API resources?
Good day, please tell me:
There is the first json resource:
<?php
namespace App\Http\Resources\Infobject;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
class InfobjectWithWayLineResource extends JsonResource
{
public function toArray($request)
{
return [
'id' => $this->id,
'label' => $this->name . ' (' . $this->code . ')',
'color' => $this->color,
'children' => InfobjectResource::collection($this->whenLoaded('infobjects'))
];
}
}
<?php
namespace App\Http\Resources\Infobject;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
class InfobjectResource extends JsonResource
{
public function toArray($request)
{
return [
'id' => $this->id,
'label' => $this->type . ' ' . $this->name,
'color' => '???'
];
}
}
Answer the question
In order to leave comments, you need to log in
create a custom class InfobjectResourceCollection extends ResourceCollection
and pass properties on each element, it will be something like this
class InfobjectResourceCollection extends ResourceCollection{
public $collects = InfobjectResource::class;
public function toArray($request)
{
return $this->collection->map
->additional($this->additional)
//можно прокинуть additional от коллекции, либо задать свойство в конструкторе коллекции
->map
->toArray($request)->all();
}
}
Thanks, that's what I did, and here's the result:
<?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)
];
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question