Home > front end >  Why my resource on Laravel is not working as expected?
Why my resource on Laravel is not working as expected?

Time:10-26

I have a simple resource:

<?php

namespace App\Http\Resources\Integration\PayGo;

use Illuminate\Http\Resources\Json\JsonResource;

class VendaVenderResource extends JsonResource
{
    public $preserveKeys = true;

    protected $method;

    public function paymentMethod(string $method = null)
    {
        $this->method = $method;

        return $this;
    }

    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        dd("ENTROU");
        return [
            'id' => $this->id,
        ];
    }
}

When i made a simple service that call this Resource in the middle of the code like this:

    $vendaVenderResource = (new Resources\Integration\PayGo\VendaVenderResource($entry));

    dd("PASSOU");

This method toArray() from Resource is not executing and is not entering in my dd("ENTROU"); for testing purpose, it is executing this dd("PASSOU"); after the Resource i don't know why!!

What i'm doing wrong?

CodePudding user response:

You can't call your resource this way. Resources should be called thought an api (so the accept json will be sent, and laravel should know what to return ....

If you want to use resources in this way, you should use the method resolve (new Resources\Integration\PayGo\VendaVenderResource($entry))->resolve();

Ps.: Entrou e passou are so huebrbr kkkkkk

  • Related