I am trying to get the sum of a property inside an each loop. Here is my code:
$client_systems_count = 0;
$client->accounts->each(function ($account) use ($client_systems_count) {
dd($account->systems->count());
$client_systems_count += $account->systems->count();
});
dump($client_systems_count);
Any idea why it isn't working? I assume that even though I change the $client_systems_count variable inside the loop every time it goes to the next element inside the collection it resets to it's initial value. So which is the correct approach to get the total systems count?