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

jogesh_pi's avatar

Laravel Resource API returning with[] and addtional data

Hi, I am getting strange behavior when going to implement Laravel Resource API to a new project. I have a CategoryResource which is returning the following codes.

public function toArray($request)
    {
        return [
            'uuid' => $this->uuid, 
            'name' => $this->name
        ];
    }

And in ProductResource i am returning product with category like this.

public function toArray($request)
    {
        return [
            'id' => $this->id, 
            'uuid' => $this->uuid, 
            'name' => $this->title, 
            'description' => $this->description, 
            'category' => new CategoryResource($this->category), 
            'status' => $this->status
        ];
    }

*BUT with i call the Product Resource I am getting the result something like *

category: {
  additional: []
    resource: {id: "1", uuid: "de75123ea9244d648c733474f7778e8b", parent_id: null, name: "Category"}
  with: []
}
description: null
id: "1"

BUT i am expecting the result should like

category: {id: "1", uuid: "de75123ea9244d648c733474f7778e8b", parent_id: null, name: "Category"}
description: null
id: "1"

This is how i am calling resource through the controller with Datatables.

public function anyData() {
        $productResource = ProductResource::collection(Product::with('category', 'brand')->Paginate(20));
        
        return DataTables::of($productResource)->toJson();
    }

My Product Model has this relationship

public function category()
    {
        return $this->belongsTo(Category::class);
    }
0 likes
0 replies

Please or to participate in this conversation.