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

Ajvanho's avatar
Level 14

API does not load at all

I have an API, that does not load at all, not error, intinity loading. I check in Postman, same situation.

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;
use App\Http\Resources\ProductResource;

class ProductVariationResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        return [
            "id" => $this->id,
            "name" => $this->name,
            "price" => $this->formattedPrice,
            "stock_count" => $this->stock,
            "in_stock" => $this->inStock(),
            "product" => new ProductResource($this->product),
            
        ];
    }
}

The problem arose when I entered "product" => new ProductResource($this->product), It works without it, but why don't I have error?

  return [
            "id" => $this->id,
            "name" => $this->name,
            "price" => $this->formattedPrice,
            "stock_count" => $this->stock,
            "in_stock" => $this->inStock(),
        ];
0 likes
8 replies
automica's avatar

@ivanradojevic what happens here?

new ProductResource($this->product)

can you post what that resource looks like?

1 like
bugsysha's avatar

And how does your ProductResource and Product model look like? You probably have something like following in your related resource/model that creates endless loop.

'product_variation' => new ProductVariationResource($this->productVariation),
Ajvanho's avatar
Level 14

Yes, probably is that; ProductResource

return [
            "id" => $this->id,
            "name" => $this->name,
            "slug" => $this->slug, 
            "description" => $this->description, 
            "price" => $this->formattedPrice,
            "product_stock_count" => $this->productStockCount(),
            "in_product_stock" => $this->inProductStock(),
            "variations" => ProductVariationResource::collection($this->variations)->groupBy("type.name"),
            
        ];
bugsysha's avatar
bugsysha
Best Answer
Level 61

Try removing it and post back.

Ajvanho's avatar
Level 14

Can I try to make new resource for belongsTo relationship for the product:

"product" => new ProductFooResource($this->product),
bugsysha's avatar

You can try, but I do not see why would you need anything like that.

1 like
Ajvanho's avatar
Level 14

Yes, it works that way

return [
            "id" => $this->id,
            "name" => $this->name,
            "price" => $this->formattedPrice,
            "stock_count" => $this->stock,
            "in_stock" => $this->inStock(),
            "product" => new BelongsToProduct($this->product),
            
        ];

It seemed to me that it was easier for me to break down data from shopping cart, but it is not necessary.

bugsysha's avatar

@ivanradojevic then remove it. The only reason why it is working is that you do not have inverse relationship defined in it.

1 like

Please or to participate in this conversation.