bacordioroger
7 months ago

Merging Collection with Object Keys

Posted 7 months ago by bacordioroger

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 {}

Please sign in or create an account to participate in this conversation.