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

mbkkong's avatar

Is there some analog of API Resources for FrontEnd part?

Hi everybody. I have some troubles, I want to filter data before to pass it to view, but Resources is not working. Is there any idea, how I can do it, as in API Resources toArray() method?

0 likes
3 replies
Sinnbeck's avatar

If possible I would suggest you filter it in your query. Resources are for presentation.

Also its a good idea to post some code. If we can see what you are trying to do, we might be able to give some better help

mbkkong's avatar

Thank you for your reply. Okey, here you're

class ReviewResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {

        return [
            'id' => $this->id,
            'user_name' => $this->user_name,
            'user_location' => $this->user_location,
            'date' => date('d.m.Y', strtotime($this->created_at)),
            'text' => $this->text,
            'vendor_name' => $this->vendor->name,
            'vendor_rating' => $this->vendor->rating(),
            'vendor_count_reviews' => count($this->vendor->reviews),
            'vendor_logo' => $this->vendor->logo(),

        ];
    }
}

for example, if I use an API, I can do so, but I want to do the same thing using just web, to filter returning data before to send it to Blade.

Please or to participate in this conversation.