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

jgravois's avatar

Trying to get subtotals from a collection

I am lost in this collection ...

I have a bunch of records like these:

Illuminate\Database\Eloquent\Collection {#3627
     all: [
       App\Models\ArAging {#3703
         id: 3,
         amt: 1500.0,
         balance: 1500.0,
         city: "Atlanta",
         company_code: "DLA",
         company_name: "XXX Airlines ",
         due_date: "2021-01-21",
         due_days: 45,
         entry_date: "2020-12-07",
         max_credit: 3300000.0,
         term_code: "NET 45",
       },
       App\Models\ArAging {#4202
         id: 31,
         amt: 1250000.0,
         balance: 1250000.0,
         city: "Bismark",
         company_code: "AZI3",
         company_name: "XXX Engines",
         due_date: "2020-12-10",
         due_days: 60,
         entry_date: "2020-11-28",
         max_credit: 50000000.0,
         term_code: "NET 60",
       },
       App\Models\ArAging {#4246
         id: 76,
         amt: 800.0,
         balance: 2300.0,
         city: "Atlanta",
         company_code: "DLA",
         company_name: "XXX Airlines ",
         due_date: "2020-12-12",
         due_days: 45,
         entry_date: "2020-10-28",
         max_credit: 3300000.0,
         term_code: "NET 45",
       }]

You can see the companies can have one or more records and they are mixed up. I am trying to extract a "subtotal" line by company.

$reporter = [];
$raw = ArAging::all();

$companies = $raw->pluck('company_code')->unique();

foreach($companies as $company) {
	$comp = $raw->filter(fn($co)=>$co->company_code == $company);
  
  	$code = $comp->pluck('company_code')->unique();

	$name = $comp->pluck('company_name')->unique();

	$credit_limit = $comp->pluck('max_credit')->unique();

	$terms = $comp->pluck('term_code')->unique();
  
  	$sales = $comp->sum('amt');
  
  	$balance = $comp->last()->balance;
  
  $reporter[] = [
  	'company_code' => $code,
    'company_name' => $name,
    'credit_limit' => $credit_limit,
    'terms' => $terms,
    'sales' => $sales,
    'balance' => $balance
  ];
} // end foreach

return $reporter;

This is returning

[!] Aliasing 'ArAging' to 'App\Models\ArAging' for this Tinker session.
=> [
     "company_code" => Illuminate\Support\Collection {#4247
       all: [
         "DLA",
       ],
     },
     "company_name" => Illuminate\Support\Collection {#4127
       all: [
         "XXX Airlines ",
       ],
     },
     "credit_limit" => Illuminate\Support\Collection {#4041
       all: [
         3300000.0,
       ],
     },
     "terms" => Illuminate\Support\Collection {#4146
       all: [
         "NET 45",
       ],
     },
     "sales" => 4800.0,
     "balance" => 2300.0,
   ]

rather than what I am looking for

[
    'company_code' => 'DLA',
    'company_name' => 'XXX Airlines',
    'credit_limit' => 3300000.0,
    'terms' => 'NET 45',
    'sales' => 4800.0,
    'balance' => 2300.0
  ]
0 likes
1 reply
Snapey's avatar
Snapey
Best Answer
Level 122

Sounds like you need to use GroupBy in the query or in the collection. You can then summarise the records that are grouped together.

Please or to participate in this conversation.