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

Lara_Love's avatar

@php $total += $details['price'] * $details['quantity'] @endphp

hi i use in laravel this code. before day's its work . but now dosn't work

https://postimg.cc/dLz5PzXV

 @php $total = 0 @endphp
                    @if(session('cart'))
                        @foreach(session('cart') as $id => $details)
                            @php $total += $details['price'] * $details['quantity'] @endphp
                            <tr data-id="{{ $id }}">

what is error ?

0 likes
8 replies
Sergiu17's avatar
// define $total variable.
@php $total = 0 @endphp
@php
  $total += $details['price'] * $details['quantity']
@endphp
$total += 10;
is equivalent to
$total = $total + 10; // but $total is undefined
Lara_Love's avatar

@Sergiu17 its define

 @php $total = 0 @endphp
                    @if(session('cart'))
                        @foreach(session('cart') as $id => $details)
                            @php $total += $details['price'] * $details['quantity'] @endphp
                            <tr data-id="{{ $id }}">
tykus's avatar

@LoverToHelp it this just an IDE "error"?

You could compute the total outside the loop:

{{ array_reduce(session('cart'), fn ($total, $item) => $total += $item['price'] * $item['quantity'], 0) }}

Although, I'd suggest calculating this value in the Controller rather that in the Blade template

1 like
tykus's avatar

You have not initialized $total

@php($total = 0)
@foreach (session('cart') as $id => $details)
    @php $total += $details['price'] * $details['quantity'] @endphp
Lara_Love's avatar

@tykus we together make for it factor , it's do currect

@php $total = 0 @endphp
@if(session('cart'))
@foreach(session('cart') as $id => $details)
@php $total += $details['price'] * $details['quantity'] @endphp
<tr data-id="{{ $id }}">
    ```
    but now cant update for total

    ```
    @php $total = 0 @endphp
    @if(session('cart'))
    @foreach(session('cart') as $id => $details)
    @php $total += $details['price'] * $details['quantity'] @endphp
<tr data-id="{{ $id }}">
    <p class="font18 text-right">{{ $details['name'] }}</p>
    <p class="font18">{{ $details['price'] }} </p>
    <input style="height:40px;width:100%;font-size: 20px;" type="number" value="{{ $details['quantity'] }}"
           class="form-control quantity update-cart"/>
    {{ $details['price'] * $details['quantity'] }}
    <td class="align-middle">
        <button class=" remove-from-cart"><i class="fa fa-close font20"></i></button>
        @endforeach
        @endif
        {{ number_format($total) }}
        @include('partfront.footer')
        @endsection
        @section('scripts')
        <script type="text/javascript">
            $(".update-cart").change(function (e) {
                e.preventDefault();
                var ele = $(this);
                $.ajax({
                    url: '{{ route('update.cart') }}',
                    method: "patch",
                    data: {
                        _token: '{{ csrf_token() }}',
                        id: ele.parents("tr").attr("data-id"),
                        quantity: ele.parents("tr").find(".quantity").val()
                    },
                    success: function (response) {
                        window.location.reload();
                    }
                });
            });

            $(".remove-from-cart").click(function (e) {
                e.preventDefault();

                var ele = $(this);

                if (confirm("It's Delete")) {
                    $.ajax({
                        url: '{{ route('remove.from.cart') }}',
                        method: "DELETE",
                        data: {
                            _token: '{{ csrf_token() }}',
                            id: ele.parents("tr").attr("data-id")
                        },
                        success: function (response) {
                            window.location.reload();
                        }
                    });
                }
            });

        </script>
        ```
tykus's avatar
tykus
Best Answer
Level 104

@LoverToHelp remember, no ¯\_(ツ)_/¯

Anyway, is this just an IDE error, or is it not working in the Browser?

1 like

Please or to participate in this conversation.