php – Laravel: How I can remove inner arrays from my collection?

I need to get the products that are ordered most often. I can get products from all orders this way:

$orders = $business->orders()->with('products')->get();
$subset = $orders->map(function ($order) {
      return $order->products->map(function ($product) {
             return collect($product);
      });
});
return $subset;

It returns me the next response:

[
    [
        {
            "id": "94567311-e669-4943-b90e-537669099bc2",
            "business_order_id": "94567311-e554-4265-8753-db5d021f0544",
            "business_product_id": "92df0ca8-762d-4caf-a3af-ea0359906338",
            "stock_keeping_unit": null,
            "name": "Black Tote Bag",
            "description": "Small",
            "weight": null,
            "length": null,
            "width": null,
            "depth": null,
            "variation_key_1": "Size",
            "variation_value_1": "Small",
            "variation_key_2": null,
            "variation_value_2": null,
            "variation_key_3": null,
            "variation_value_3": null,
            "quantity": 1,
            "tax_name": null,
            "tax_rate": "0.0000",
            "unit_price": 1500,
            "tax_amount": 0,
            "discount_amount": 0,
            "price": 1500,
            "remark": null,
            "business_image_id": "92516d93-e06c-4718-b49a-8d63844ce5e5",
            "created_at": "2021-09-07 12:49:46",
            "updated_at": "2021-09-07 12:49:46",
            "deleted_at": null
        },
        {
            "id": "94567311-e6bb-48b4-8bdf-ad282dcc6d72",
            "business_order_id": "94567311-e554-4265-8753-db5d021f0544",
            "business_product_id": "92df0ca8-7781-4d9a-859e-8296669a9926",
            "stock_keeping_unit": null,
            "name": "Black Tote Bag",
            "description": "Medium",
            "weight": null,
            "length": null,
            "width": null,
            "depth": null,
            "variation_key_1": "Size",
            "variation_value_1": "Medium",
            "variation_key_2": null,
            "variation_value_2": null,
            "variation_key_3": null,
            "variation_value_3": null,
            "quantity": 1,
            "tax_name": null,
            "tax_rate": "0.0000",
            "unit_price": 1500,
            "tax_amount": 0,
            "discount_amount": 0,
            "price": 1500,
            "remark": null,
            "business_image_id": "92516d93-e06c-4718-b49a-8d63844ce5e5",
            "created_at": "2021-09-07 12:49:46",
            "updated_at": "2021-09-07 12:49:46",
            "deleted_at": null
        }
    ],
    [
        {
            "id": "94606929-ad05-4c67-9491-644e8d3b434b",
            "business_order_id": "94606929-ab27-49c1-b8da-21cefee04e7a",
            "business_product_id": "92df0ca8-762d-4caf-a3af-ea0359906338",
            "stock_keeping_unit": null,
            "name": "Black Tote Bag",
            "description": "Small",
            "weight": null,
            "length": null,
            "width": null,
            "depth": null,
            "variation_key_1": "Size",
            "variation_value_1": "Small",
            "variation_key_2": null,
            "variation_value_2": null,
            "variation_key_3": null,
            "variation_value_3": null,
            "quantity": 1,
            "tax_name": null,
            "tax_rate": "0.0000",
            "unit_price": 1500,
            "tax_amount": 0,
            "discount_amount": 0,
            "price": 1500,
            "remark": null,
            "business_image_id": "92516d93-e06c-4718-b49a-8d63844ce5e5",
            "created_at": "2021-09-12 11:40:21",
            "updated_at": "2021-09-12 11:40:21",
            "deleted_at": null
        },
        {
            "id": "94606929-ad66-4a69-93ae-1e291c2d7e53",
            "business_order_id": "94606929-ab27-49c1-b8da-21cefee04e7a",
            "business_product_id": "93253bf2-ce63-4a9a-b4a6-8ecaff439028",
            "stock_keeping_unit": null,
            "name": "Bynd Artisan Tote Bag Duplicate",
            "description": "Blue / Small",
            "weight": null,
            "length": null,
            "width": null,
            "depth": null,
            "variation_key_1": "Color",
            "variation_value_1": "Blue",
            "variation_key_2": "Size",
            "variation_value_2": null,
            "variation_key_3": null,
            "variation_value_3": null,
            "quantity": 1,
            "tax_name": null,
            "tax_rate": "0.0000",
            "unit_price": 2000,
            "tax_amount": 0,
            "discount_amount": 0,
            "price": 2000,
            "remark": null,
            "business_image_id": "93253bf2-e5c2-4a22-8605-81c245bcf86b",
            "created_at": "2021-09-12 11:40:21",
            "updated_at": "2021-09-12 11:40:21",
            "deleted_at": null
        }
    ],
    [
        {
            "id": "94606a50-2bac-48b8-860f-eebec27aff8d",
            "business_order_id": "94606a50-2a40-476e-bd71-50c096270174",
            "business_product_id": "93253bf2-ce63-4a9a-b4a6-8ecaff439028",
            "stock_keeping_unit": null,
            "name": "Bynd Artisan Tote Bag Duplicate",
            "description": "Blue / Small",
            "weight": null,
            "length": null,
            "width": null,
            "depth": null,
            "variation_key_1": "Color",
            "variation_value_1": "Blue",
            "variation_key_2": "Size",
            "variation_value_2": null,
            "variation_key_3": null,
            "variation_value_3": null,
            "quantity": 1,
            "tax_name": null,
            "tax_rate": "0.0000",
            "unit_price": 2000,
            "tax_amount": 0,
            "discount_amount": 0,
            "price": 2000,
            "remark": null,
            "business_image_id": "93253bf2-e5c2-4a22-8605-81c245bcf86b",
            "created_at": "2021-09-12 11:43:34",
            "updated_at": "2021-09-12 11:43:34",
            "deleted_at": null
        }
    ],
    [
        {
            "id": "9466c9d9-3131-43f7-99f3-c41ccd3308d1",
            "business_order_id": "9466c9d9-2a57-4551-b36b-294b64fb9fe4",
            "business_product_id": "93253bf2-ce63-4a9a-b4a6-8ecaff439028",
            "stock_keeping_unit": null,
            "name": "Bynd Artisan Tote Bag Duplicate",
            "description": "Blue / Small",
            "weight": null,
            "length": null,
            "width": null,
            "depth": null,
            "variation_key_1": "Color",
            "variation_value_1": "Blue",
            "variation_key_2": "Size",
            "variation_value_2": null,
            "variation_key_3": null,
            "variation_value_3": null,
            "quantity": 1,
            "tax_name": null,
            "tax_rate": "0.0000",
            "unit_price": 2000,
            "tax_amount": 0,
            "discount_amount": 0,
            "price": 2000,
            "remark": null,
            "business_image_id": "93253bf2-e5c2-4a22-8605-81c245bcf86b",
            "created_at": "2021-09-15 15:45:40",
            "updated_at": "2021-09-15 15:45:40",
            "deleted_at": null
        }
    ],
    [
        {
            "id": "9466dab9-cff6-4446-8729-2260ffe683a5",
            "business_order_id": "9466dab9-ccb5-4079-ad26-e76f5999164f",
            "business_product_id": "93253bf2-ce63-4a9a-b4a6-8ecaff439028",
            "stock_keeping_unit": null,
            "name": "Bynd Artisan Tote Bag Duplicate",
            "description": "Blue / Small",
            "weight": null,
            "length": null,
            "width": null,
            "depth": null,
            "variation_key_1": "Color",
            "variation_value_1": "Blue",
            "variation_key_2": "Size",
            "variation_value_2": null,
            "variation_key_3": null,
            "variation_value_3": null,
            "quantity": 1,
            "tax_name": null,
            "tax_rate": "0.0000",
            "unit_price": 2000,
            "tax_amount": 0,
            "discount_amount": 0,
            "price": 2000,
            "remark": null,
            "business_image_id": "93253bf2-e5c2-4a22-8605-81c245bcf86b",
            "created_at": "2021-09-15 16:32:51",
            "updated_at": "2021-09-15 16:32:51",
            "deleted_at": null
        },
        {
            "id": "9466dab9-d04d-454e-b6ce-889f2f0ce2ba",
            "business_order_id": "9466dab9-ccb5-4079-ad26-e76f5999164f",
            "business_product_id": "92df0ca8-762d-4caf-a3af-ea0359906338",
            "stock_keeping_unit": null,
            "name": "Black Tote Bag",
            "description": "Small",
            "weight": null,
            "length": null,
            "width": null,
            "depth": null,
            "variation_key_1": "Size",
            "variation_value_1": "Small",
            "variation_key_2": null,
            "variation_value_2": null,
            "variation_key_3": null,
            "variation_value_3": null,
            "quantity": 1,
            "tax_name": null,
            "tax_rate": "0.0000",
            "unit_price": 1500,
            "tax_amount": 0,
            "discount_amount": 0,
            "price": 1500,
            "remark": null,
            "business_image_id": "92516d93-e06c-4718-b49a-8d63844ce5e5",
            "created_at": "2021-09-15 16:32:51",
            "updated_at": "2021-09-15 16:32:51",
            "deleted_at": null
        }
    ],
    [
        {
            "id": "94f1443b-4bfd-474c-b7a7-1cf20e0c0acd",
            "business_order_id": "94f14437-044c-48ae-8029-21ac712fa112",
            "business_product_id": "94cf74c8-453a-45e2-a292-b8e3a57953ff",
            "stock_keeping_unit": null,
            "name": "Testt",
            "description": null,
            "weight": null,
            "length": null,
            "width": null,
            "depth": null,
            "variation_key_1": null,
            "variation_value_1": null,
            "variation_key_2": null,
            "variation_value_2": null,
            "variation_key_3": null,
            "variation_value_3": null,
            "quantity": 1,
            "tax_name": null,
            "tax_rate": "0.0000",
            "unit_price": 1000,
            "tax_amount": 0,
            "discount_amount": 0,
            "price": 1000,
            "remark": null,
            "business_image_id": null,
            "created_at": "2021-11-23 11:52:07",
            "updated_at": "2021-11-23 11:52:07",
            "deleted_at": null
        },
        {
            "id": "94f1443e-a36f-4057-a995-38e9c39910f6",
            "business_order_id": "94f14437-044c-48ae-8029-21ac712fa112",
            "business_product_id": "93c98a41-ce98-44ec-864f-cdce5b37a5f9",
            "stock_keeping_unit": null,
            "name": "yoyo toyyy",
            "description": null,
            "weight": null,
            "length": null,
            "width": null,
            "depth": null,
            "variation_key_1": null,
            "variation_value_1": null,
            "variation_key_2": null,
            "variation_value_2": null,
            "variation_key_3": null,
            "variation_value_3": null,
            "quantity": 1,
            "tax_name": null,
            "tax_rate": "0.0000",
            "unit_price": 1200,
            "tax_amount": 0,
            "discount_amount": 0,
            "price": 1200,
            "remark": null,
            "business_image_id": "94cf7aa4-0a9d-4bf6-ae43-ca71ce39eff0",
            "created_at": "2021-11-23 11:52:09",
            "updated_at": "2021-11-23 11:52:09",
            "deleted_at": null
        }
    ]
]

What I’m trying to achieve is to get response without inner arrays, so I can just group collection by business_product_id and count how many there are of each product.

Any advice will be helpful, thanks!

UPD: Some clarifications: In my app there are several businesses, each business can have multiple orders, and each order can have multiple products. In order to get all business orders including products (relation) I use $orders = $business->orders()->with('products')->get(); I need to calculate the quantity how often the product is ordered, in other words, the number of times I see each product in my orders

Another UPD: I got the proper response of what I wanted:

{
    "93253bf2-ce63-4a9a-b4a6-8ecaff439028": {
        "product": {
            "id": "94606929-ad66-4a69-93ae-1e291c2d7e53",
            "business_order_id": "94606929-ab27-49c1-b8da-21cefee04e7a",
            "business_product_id": "93253bf2-ce63-4a9a-b4a6-8ecaff439028",
            "stock_keeping_unit": null,
            "name": "Bynd Artisan Tote Bag Duplicate",
            "description": "Blue / Small",
            "weight": null,
            "length": null,
            "width": null,
            "depth": null,
            "variation_key_1": "Color",
            "variation_value_1": "Blue",
            "variation_key_2": "Size",
            "variation_value_2": null,
            "variation_key_3": null,
            "variation_value_3": null,
            "quantity": 1,
            "tax_name": null,
            "tax_rate": "0.0000",
            "unit_price": 2000,
            "tax_amount": 0,
            "discount_amount": 0,
            "price": 2000,
            "remark": null,
            "business_image_id": "93253bf2-e5c2-4a22-8605-81c245bcf86b",
            "created_at": "2021-09-12 11:40:21",
            "updated_at": "2021-09-12 11:40:21",
            "deleted_at": null
        },
        "count": 4
    },
    "92df0ca8-762d-4caf-a3af-ea0359906338": {
        "product": {
            "id": "94567311-e669-4943-b90e-537669099bc2",
            "business_order_id": "94567311-e554-4265-8753-db5d021f0544",
            "business_product_id": "92df0ca8-762d-4caf-a3af-ea0359906338",
            "stock_keeping_unit": null,
            "name": "Black Tote Bag",
            "description": "Small",
            "weight": null,
            "length": null,
            "width": null,
            "depth": null,
            "variation_key_1": "Size",
            "variation_value_1": "Small",
            "variation_key_2": null,
            "variation_value_2": null,
            "variation_key_3": null,
            "variation_value_3": null,
            "quantity": 1,
            "tax_name": null,
            "tax_rate": "0.0000",
            "unit_price": 1500,
            "tax_amount": 0,
            "discount_amount": 0,
            "price": 1500,
            "remark": null,
            "business_image_id": "92516d93-e06c-4718-b49a-8d63844ce5e5",
            "created_at": "2021-09-07 12:49:46",
            "updated_at": "2021-09-07 12:49:46",
            "deleted_at": null
        },
        "count": 3
    },
    "92df0ca8-7781-4d9a-859e-8296669a9926": {
        "product": {
            "id": "94567311-e6bb-48b4-8bdf-ad282dcc6d72",
            "business_order_id": "94567311-e554-4265-8753-db5d021f0544",
            "business_product_id": "92df0ca8-7781-4d9a-859e-8296669a9926",
            "stock_keeping_unit": null,
            "name": "Black Tote Bag",
            "description": "Medium",
            "weight": null,
            "length": null,
            "width": null,
            "depth": null,
            "variation_key_1": "Size",
            "variation_value_1": "Medium",
            "variation_key_2": null,
            "variation_value_2": null,
            "variation_key_3": null,
            "variation_value_3": null,
            "quantity": 1,
            "tax_name": null,
            "tax_rate": "0.0000",
            "unit_price": 1500,
            "tax_amount": 0,
            "discount_amount": 0,
            "price": 1500,
            "remark": null,
            "business_image_id": "92516d93-e06c-4718-b49a-8d63844ce5e5",
            "created_at": "2021-09-07 12:49:46",
            "updated_at": "2021-09-07 12:49:46",
            "deleted_at": null
        },
        "count": 1
    },
    "94cf74c8-453a-45e2-a292-b8e3a57953ff": {
        "product": {
            "id": "94f1443b-4bfd-474c-b7a7-1cf20e0c0acd",
            "business_order_id": "94f14437-044c-48ae-8029-21ac712fa112",
            "business_product_id": "94cf74c8-453a-45e2-a292-b8e3a57953ff",
            "stock_keeping_unit": null,
            "name": "Testt",
            "description": null,
            "weight": null,
            "length": null,
            "width": null,
            "depth": null,
            "variation_key_1": null,
            "variation_value_1": null,
            "variation_key_2": null,
            "variation_value_2": null,
            "variation_key_3": null,
            "variation_value_3": null,
            "quantity": 1,
            "tax_name": null,
            "tax_rate": "0.0000",
            "unit_price": 1000,
            "tax_amount": 0,
            "discount_amount": 0,
            "price": 1000,
            "remark": null,
            "business_image_id": null,
            "created_at": "2021-11-23 11:52:07",
            "updated_at": "2021-11-23 11:52:07",
            "deleted_at": null
        },
        "count": 1
    },
    "93c98a41-ce98-44ec-864f-cdce5b37a5f9": {
        "product": {
            "id": "94f1443e-a36f-4057-a995-38e9c39910f6",
            "business_order_id": "94f14437-044c-48ae-8029-21ac712fa112",
            "business_product_id": "93c98a41-ce98-44ec-864f-cdce5b37a5f9",
            "stock_keeping_unit": null,
            "name": "yoyo toyyy",
            "description": null,
            "weight": null,
            "length": null,
            "width": null,
            "depth": null,
            "variation_key_1": null,
            "variation_value_1": null,
            "variation_key_2": null,
            "variation_value_2": null,
            "variation_key_3": null,
            "variation_value_3": null,
            "quantity": 1,
            "tax_name": null,
            "tax_rate": "0.0000",
            "unit_price": 1200,
            "tax_amount": 0,
            "discount_amount": 0,
            "price": 1200,
            "remark": null,
            "business_image_id": "94cf7aa4-0a9d-4bf6-ae43-ca71ce39eff0",
            "created_at": "2021-11-23 11:52:09",
            "updated_at": "2021-11-23 11:52:09",
            "deleted_at": null
        },
        "count": 1
    }
}

And my final code looks like this:

$orders = $business->orders()->with('products')->get();

        foreach ($orders as $order){
            foreach ($order->products as $product)
                $subset[] = $product;
        }

        $groupedOrderedProducts = collect($subset)->groupBy('business_product_id');

        $withCount = $groupedOrderedProducts->map(function ($product){
            return ['product' => $product->first(),'count'=>$product->count()];
        })->sortByDesc('count');

But I believe it’s not the best solution, any idea of ​​how I can improve my code?

Leave a Comment