Reputation: 27
I want to calculate the total price. How can I do it?
This is my blade code:
@foreach ($ventes as $value)
<td>{{ $value->produit->nom }}</td>
<td>
{{ $value->quantitevendu }}</a>
</td>
<td>
({{$value->prix}})*{{$value->quantitevendu}}.euro
</td>
<td>
({{$value->prix}}).DH
</td>
<td>
{{ $value->description }}
</td>
</tr>
@endforeach
It gives me results like this:
(10)*2
(11)*4
(2)*1
But I want results like this:
20
44
2
Upvotes: 0
Views: 1899
Reputation: 5603
It's best practice to put heavy calculation within your Model. Laravel allow to create imaginary attributes event when they doesn't really exist on your table. for that your can just define an attribute accessor in which you perform all your calculation like bellow
class Vente extends Model {
public function getPrixTotalAttribute()
{
return $this->attributes['prix'] * $this->attributes['quantitevendu'];
}
}
And in your view you will have just to do {{ $value->prix_total}}
Upvotes: 1
Reputation: 3594
You did concatenation instead of multiplication. Try this:
@foreach ($ventes as $value)
<td>{{ $value->produit->nom }}</td>
<td>
{{ $value->quantitevendu }}</a>
</td>
<td>{{$value->prix * $value->quantitevendu}} .euro</td>
<td>({{$value->prix}}).DH</td>
<td>{{ $value->description }}</td>
</tr>
@endforeach
I hope you get it the idea.
Upvotes: 3