Eden WebStudio
Eden WebStudio

Reputation: 788

Make array_sum add total in each foreach loop in Laravel PHP.

I have a list of clients who have many appointments in db. I want to iterate through the appointments totalling their duration for each client so I can create an invoice. When I run the below code the first client's appointments duration are added correctly. However my issue is, for the next client it seems to add the first clients duration too. Is array_sum the correct method?? What do i need to use if not?

foreach($clients as $client)
        { 
            $appointments = Client::find($client->id)->appointments()->get();

                foreach($appointments as $appointment)
                {

                    $date1      = $appointment->starts_at; 
                    $date2      = $appointment->ends_at;                        
                    $start      = Carbon::parse($date1);
                    $end        = Carbon::parse($date2);

                    $length     = $start->diffinhours($end);
                    $duration[] = $length;          //is this correct?
                    $total = array_sum($duration);    //is this correct?
                }

            $invoice                    = new Invoice;
            $invoice->total_hours       = $total;   
            dd($invoice);
            $invoice->save();   
        }

Upvotes: 2

Views: 1632

Answers (2)

Alexey Mezenin
Alexey Mezenin

Reputation: 163958

Yes you can use this method, but you need to move it outside the loop and also you should empty $total with each iteration:

....
$duration = [];

foreach ($appointments as $appointment)
{
    $date1      = $appointment->starts_at; 
    $date2      = $appointment->ends_at;                        
    $start      = Carbon::parse($date1);
    $end        = Carbon::parse($date2);

    $length     = $start->diffinhours($end);
    $duration[] = $length;
}

$total = array_sum($duration);
....

Also, you could add start_at and end_at timestamps to the $dates array in a model and use this code instead:

foreach ($appointments as $appointment)
{
    $duration[] = $appointment->starts_at->diffInHours($appointment->ends_at);
}

Upvotes: 1

labue
labue

Reputation: 2623

Reset the duration for each client and array_sum after the appointment loop:

foreach ($clients as $client)
{ 
    $appointments = Client::find($client->id)->appointments()->get();

    $duration = array();

    foreach ($appointments as $appointment)
    {
        $date1      = $appointment->starts_at; 
        $date2      = $appointment->ends_at;                        
        $start      = Carbon::parse($date1);
        $end        = Carbon::parse($date2);

        $length     = $start->diffinhours($end);
        $duration[] = $length;
    }

    $total = array_sum($duration);

    $invoice                    = new Invoice;
    $invoice->total_hours       = $total;   
    dd($invoice);
    $invoice->save();   
}

Upvotes: 2

Related Questions