vendredi 19 octobre 2018

Is there an easier way to filter columns in a Laravel Resource Collection?

<?php

namespace App\Http\Resources\Moderate;

use Illuminate\Http\Resources\Json\ResourceCollection;

class MoveSearchCollection extends ResourceCollection
{
    /**
     * Transform the resource collection into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        foreach($this->collection as $key => $value) {
            $this->collection[$key] = $this->collection[$key]->only(['id', 'name']);
        }

        return [
            'data' => $this->collection,
        ];
    }
}

I have the collection above and can filter it, but is there an easier way to only include certain fields?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire