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

bacordioroger's avatar

Laravel API Resource

Hi, I'm making API resource from laravel.

This is my first function:

    public function sampleCategories(){
            $inventories = CategoriesController::getUserInventory();
            return new Sample2Resource($inventories);
    }

My Sample2Resource:

        return parent::toArray($request);

Output

{
  "data": [
    {
      "id": 1,
      "user_id": 1,
      "sub_category_id": 6,
      "created_at": "2019-03-08 03:00:00",
      "updated_at": "2019-03-08 03:00:00",
      "deleted_at": null,
      "get_inventory_details": {
        "id": 1,
        "inventory_id": 1,
        "image_path": "Pahiram-Drone1-1.png",
        "name": "Drone i1",
        "description": "Drone i1 The Best Drone yet!",
        "quantity": 10,
        "cost_per_day": 1000,
        "cost_per_hour": 100,
        "status": "0",
        "created_at": "2019-03-08 03:00:00",
        "updated_at": "2019-03-08 03:00:00",
        "deleted_at": null
      }
    },
    {
      "id": 2,
      "user_id": 2,
      "sub_category_id": 4,
      "created_at": "2019-03-08 03:00:00",
      "updated_at": "2019-03-08 03:00:00",
      "deleted_at": null,
      "get_inventory_details": {
        "id": 2,
        "inventory_id": 2,
        "image_path": "Pahiram-Drone2.png",
        "name": "Drone i6 Pro",
        "description": "Drone i6 Pro The Best Drone yet!",
        "quantity": 14,
        "cost_per_day": 1100,
        "cost_per_hour": 100,
        "status": "0",
        "created_at": "2019-03-08 03:00:00",
        "updated_at": "2019-03-08 03:00:00",
        "deleted_at": null
      }
    },
    {
      "id": 3,
      "user_id": 3,
      "sub_category_id": 5,
      "created_at": "2019-03-08 03:00:00",
      "updated_at": "2019-03-08 03:00:00",
      "deleted_at": null,
      "get_inventory_details": {
        "id": 3,
        "inventory_id": 3,
        "image_path": "Pahiram-Drone3.png",
        "name": "Drone i7",
        "description": "Drone i7 The Best Drone yet!",
        "quantity": 18,
        "cost_per_day": 1500,
        "cost_per_hour": 100,
        "status": "0",
        "created_at": "2019-03-08 03:00:00",
        "updated_at": "2019-03-08 03:00:00",
        "deleted_at": null
      }
    },
    {
      "id": 4,
      "user_id": 4,
      "sub_category_id": 5,
      "created_at": "2019-03-08 03:00:00",
      "updated_at": "2019-03-08 03:00:00",
      "deleted_at": null,
      "get_inventory_details": {
        "id": 4,
        "inventory_id": 4,
        "image_path": "Pahiram-Drone4.png",
        "name": "Drone i7X",
        "description": "Drone i7X The Best Drone yet!",
        "quantity": 16,
        "cost_per_day": 1900,
        "cost_per_hour": 100,
        "status": "0",
        "created_at": "2019-03-08 03:00:00",
        "updated_at": "2019-03-08 03:00:00",
        "deleted_at": null
      }
    },
    {
      "id": 5,
      "user_id": 5,
      "sub_category_id": 8,
      "created_at": "2019-03-08 03:00:00",
      "updated_at": "2019-03-08 03:00:00",
      "deleted_at": null,
      "get_inventory_details": {
        "id": 5,
        "inventory_id": 5,
        "image_path": "Pahiram-Drone5.png",
        "name": "Drone i7 Plus",
        "description": "Drone i7 Plus The Best Drone yet!",
        "quantity": 11,
        "cost_per_day": 1300,
        "cost_per_hour": 100,
        "status": "0",
        "created_at": "2019-03-08 03:00:00",
        "updated_at": "2019-03-08 03:00:00",
        "deleted_at": null
      }
    }
  ]
}

This is my second function:

   public function userCategories(){
            $inventories = CategoriesController::getUserInventory();
            $categories = CategoriesController::getUserSubCategoriesDetails();
            $bookings = CategoriesController::getUserBookings();
            $all = collect();
            $all->put('inventories', $inventories);
            $all->put('categories', $categories);
            $all->put('bookings', $bookings);
            return new SampleResource($all);
      }

Sample Resource:

        return $this->resource['inventories'];

Output is the same from above (all even brackets and braces), but when I tried:

        return [
            'id' => $this->resource['inventories']->id
        ];

It says

Property [id] does not exist on this collection instance.
0 likes
1 reply
Kisiara's avatar

What output do you get when you use

$this->resource['inventories']->first()->id

I assume that since the SampleResource returns a collection and not a single item then you do not have access to the id property.

Please or to participate in this conversation.