@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 ?
// 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
@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 }}">
@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
You have not initialized $total
@php($total = 0)
@foreach (session('cart') as $id => $details)
@php $total += $details['price'] * $details['quantity'] @endphp
@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 you write for this session an uuid. are you remember
@LoverToHelp remember, no ¯\_(ツ)_/¯
Anyway, is this just an IDE error, or is it not working in the Browser?
Please sign in or create an account to participate in this conversation.