how to display the total from the price calculation

Viewed 35

for example on row one the price is 2,580,000 and on the second row 950,000 well I want to show the total sum of all prices to be 3530000,this is my view code :

@forelse ($bookings as $booking)
                                @php
                                    $check_out= date_create($booking['check_out']);
                                    $check_in = date_create($booking['check_in']);
                                    $calculate = date_diff($check_out, $check_in);
                                    $day = $calculate->format("%a");
                                    $price = $day * $booking->room->price;
                                @endphp
                                <tbody>
                                    <th>{{ $loop->iteration + $bookings->firstItem() - 1 . '.' }}</th>
                                    <td><u>{{ $booking->booking_code }}</u></td>
                                    <td>
                                        <span class="badge badge-light">{{ "Rp. " . number_format($price, 0,',','.') }}<span>
                                    </td>
                                </tbody>
                            @empty
                                <tbody>
                                    <tr>
                                        <th colspan="3" style="color: red; text-align: center;">Data Empty!</th>
                                    </tr>
                                </tbody>
                            @endforelse
                            <tfoot>
                                <tr>
                                    <th>TOTAL</th>
                                    <td></td>
                                    <td>{{ $price + $price }}</td>
                                </tr>
                            </tfoot>
1 Answers

You can sum $price to $total variable and use them later.

@php
    ...
    $price = $day * $booking->room->price;
    $total = ($total ?? 0) + $price;
@endphp

<tfoot>
    <tr>
        <th>TOTAL</th>
        <td></td>
        <td>{{ $total }}</td>
    </tr>
</tfoot>
Related