Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

farshadf's avatar

how to customize api with resource on collection

i have a collection on my controller as below :


 $data = Accommodation::with(['city','accommodationRoomsLimited.roomPricingHistorySearch' =>function($query) use($from_date,$to_date){
                $query->whereDate('from_date', '<=', $from_date);
                $query->whereDate('to_date', '>=', $to_date);
                }])    
                ->get();
            return new FilterResource($data);

and here is my filter resource


  public function toArray($request)
    {
//        return parent::toArray($request);
        return [
            'id' => $this->id,
    'costumfields' => 'somecostumfields'
        ];
    }

now i want to customize my resource so that i can add some custom fields to it and customize the current fields on the api but this code gives me the error below :


"message": "Property [id] does not exist on this collection instance.",
0 likes
1 reply
Sti3bas's avatar
Sti3bas
Best Answer
Level 53

return new FilterResource($data); should be return FilterResource::collection($data);

1 like

Please or to participate in this conversation.