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

bacordioroger's avatar

Merging Collection with Object Keys

Hi, I'm trying to merge laravel collection and I want to add keys.

This is my code:

            $all = collect();
            $all->put('inventories', $inventories);
            $all->put('categories', $categories);
            $all->put('bookings', $bookings);
            return new SampleResource($all);

This is my desired output:

{
  "data": {
    "inventories": {
      {
        "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
        }
      }
    },
    "categories": {
      {
        "id": 1,
        "category_id": 1,
        "parent_id": null,
        "created_at": "2019-03-08 03:00:00",
        "updated_at": "2019-03-08 03:00:00",
        "deleted_at": null,
        "get_sub_category_details": {
          "id": 1,
          "sub_category_id": 1,
          "category_type_id": 1,
          "name": "Drone DJI 1",
          "description": "Drone DJI 1",
          "created_at": "2019-03-08 03:34:23",
          "updated_at": "2019-03-08 03:34:23",
          "deleted_at": null
        }
      }
    },
    "bookings": {}
  }
}

This is my current output:

{
  "data": {
    "inventories": [
      {
        "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
        }
      }
    ],
    "categories": [
      {
        "id": 1,
        "category_id": 1,
        "parent_id": null,
        "created_at": "2019-03-08 03:00:00",
        "updated_at": "2019-03-08 03:00:00",
        "deleted_at": null,
        "get_sub_category_details": {
          "id": 1,
          "sub_category_id": 1,
          "category_type_id": 1,
          "name": "Drone DJI 1",
          "description": "Drone DJI 1",
          "created_at": "2019-03-08 03:34:23",
          "updated_at": "2019-03-08 03:34:23",
          "deleted_at": null
        }
      }
    ],
    "bookings": []
  }
}

As you can see it returns array. I want it as objects. Replaced [] with {}

0 likes
1 reply
burlresearch's avatar

I think you're a little incorrect here. It looks like you're creating probably an API Resource out of your collection, and that would "return" a \Illuminate\Http\Resources\Json\JsonResource.

This is not an array, but can be converted to one. So likely you're looking at the output of a controller function, which is converting your API Resource into an array, which is converted to JSON, and shown to you in a browser?

If you want it converted to a PHP Object, then I suppose you could cast it, but I suspect that's actually what you want.

$sampleRes = new SampleResource($all);
$obj = (object) $sampleRes->toArray();

will give you an object, but maybe you can explain what you are doing - this looks like you are a little confused?

Please or to participate in this conversation.