wavrik
wavrik

Reputation: 361

Why my resource on Laravel is not working as expected?

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?

Upvotes: 0

Views: 964

Answers (1)

Samuel Aiala Ferreira
Samuel Aiala Ferreira

Reputation: 694

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

Upvotes: 2

Related Questions