S
S
SuroviyD2020-09-11 15:40:46
Laravel
SuroviyD, 2020-09-11 15:40:46

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'))
        ];
    }
}

And the second one:
<?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' => '???'
        ];
    }
}

How to cast color property from InfobjectWithWayLineResource to InfobjectResource ?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Samuel_Leonardo, 2020-09-12
@SuroviyD

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();
    }

}

there are no built-in methods for this

S
SuroviyD, 2020-09-17
@SuroviyD

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);
    }
}

The forward itself:
<?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 question

Ask a Question

731 491 924 answers to any question