Reputation: 245
I have 5 arrays, each array contains medicine_price
, medicine_quantity
.
What I want is to add the result of each array.
The problem is, I cant get the correct result of each array.
Example:
result1 = medicine_price * medicine_quantity
result2 = medicine_price * medicine_quantity
result3 = medicine_price * medicine_quantity
result4 = medicine_price * medicine_quantity
result5 = medicine_price * medicine_quantity
total = result1 + result2 + result3 + result4 + result5
This should be the result that I want.
And it is also possible that more array will be included since I have incremented them.
Code :
@php
$i = 1;
@endphp
@foreach($carts as $cart)
{!! $cart->medicine_quantity !!}
@php
$value= $cart->medicine_price * $cart->medicine_quantity;
echo "<input type='hidden' id='sample$i' value='$value'>";
$i++
@endphp
@endforeach
@php
echo "$Total_price"
@endphp
Upvotes: 4
Views: 21559
Reputation:
There is a more clean way to do this
Use mutators for a cart total
class Cart extends Model
{
protected $appends = ['total_price'];
public function getTotalPriceAttribute()
{
return $this->medicine_price * $this->medicine_quantity;
}
}
Then access it $cart->total_price
And for carts total, you could use collection method sum
like this
$total = $carts->sum('total_price');
Your blade view will be something like this
@foreach($carts as $cart)
{{ $cart->medicine_quantity }}
<input type='hidden' id='sample{{ $loop->index }}' value='{{ $cart->total_price }}'>
@endforeach
{{ $carts->sum('total_price') }}
Upvotes: 2
Reputation: 219
<div class="mx-auto"><strong>Total Number of Voters</strong> <p class="mx-3">{{$list->sum('voters')}}</p></div>
Upvotes: 5