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

leinad95's avatar

sum() in a collection of collections - nested attributes

Hello! Could somebody please help with a situation I have?

I have two tables. An orders table and and orders_items table that contains the lines of an order (usually products in a specific order) and because I already have a query to my DB and all the data already in a collection from a previous operation, I want to do a sum of the product_quantity filed to sum up all the products needed for those orders.

This is the eloquent query: Order::where('operator_id', Auth::user()->id)->whereMonth('order_date', '=', date('m'))->whereYear('order_date', '=', date('Y'))->with('items')->get();

Relationship here is an order has many items.

and this is the collection I get from that query:

Collection {#299 ▼ #items: array:9 [▼ 0 => Order {#269 ▼ #table: "bdm_orders" #primaryKey: "order_id" +timestamps: false #connection: null #keyType: "int" #perPage: 15 +incrementing: true #attributes: array:23 [▼ "order_id" => "12596" "order_unique_id" => "ORDR-fhn28q9juxkbmdb8xtfzo0psubh2n" "user_id" => "k3hpo9ui397j83aj7h8xesufb665n" "operator_id" => "47" "medic_id" => "55" "order_total" => "209.00" "transport_tax" => "20.00" "order_currency" => "RON" "order_status" => "C" "order_date" => "2017-04-03 09:40:51" "order_delivery_date" => "2017-04-03 09:40:51" "order_pay_date" => "2017-04-03 16:32:42" "order_weight" => "0.10" "bill_id" => "AYU00012244" "courier_id" => "1" "company_id" => "1" "awb" => "AYU45447926T" "dpd_nt" => "0" "otto_nt" => "0" "otto_nt_sent" => "0" "optim_nt" => "0" "optim_nt_sent" => "0" "deleted" => "0" ] #original: array:23 [▶] #relations: array:1 [▼ "items" => Collection {#297 ▼ #items: array:2 [▼ 0 => OrderItem {#300 ▼ #table: "bdm_order_item" #primaryKey: "order_item_id" +timestamps: false #connection: null #keyType: "int" #perPage: 15 +incrementing: true #attributes: array:7 [▼ "order_item_id" => "28872" "order_id" => "ORDR-fhn28q9juxkbmdb8xtfzo0psubh2n" "product_id" => "131" "order_item_name" => "Cordyceps - 180 cps" "product_quantity" => "1" "product_item_price" => "0.00" "order_item_date" => "2017-04-03 09:40:51" ] #original: array:7 [▶] #relations: [] #hidden: [] #visible: [] #appends: [] #fillable: [] #guarded: array:1 [▶] #dates: [] #dateFormat: null #casts: [] #touches: [] #observables: [] #with: [] #morphClass: null +exists: true +wasRecentlyCreated: false } 1 => OrderItem {#301 ▶} ] } ] #hidden: [] #visible: [] #appends: [] #fillable: [] #guarded: array:1 [▶] #dates: [] #dateFormat: null #casts: [] #touches: [] #observables: [] #with: [] #morphClass: null +exists: true +wasRecentlyCreated: false } 1 => Order {#270 ▶} 2 => Order {#271 ▶} 3 => Order {#272 ▶} 4 => Order {#273 ▶} 5 => Order {#274 ▶} 6 => Order {#275 ▶} 7 => Order {#276 ▶} 8 => Order {#277 ▶} ] }

I basically want to make a sum of all the "product_quantity" field and don't wanna use a foreach approach but take advantage of the aggregate methods that laravel offers and not make a mess out of this with line and lines of code iterating kind of manually.

Thank You! :)

0 likes
6 replies
leinad95's avatar

Thanks for the quick answer!

It works if I want to count the number of lines (different products) for an order but each line in the order has different quantity, not necessarily one item and I want to sum that product_quantity filed in the items table.

tykus's avatar
tykus
Best Answer
Level 104
$orders = Order::where('operator_id', Auth::user()->id)
    ->whereMonth('order_date', '=', date('m'))
    ->whereYear('order_date', '=', date('Y'))
    ->with('items')
    ->get();

$totalQuantity = $orders->sum(function ($order) {
    return $order->items->sum('product_quantity');
});

Please or to participate in this conversation.