fouadmad
fouadmad

Reputation: 27

How to calculate the total of price for product in Laravel blade

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

Answers (2)

Yves Kipondo
Yves Kipondo

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

Elisha Senoo
Elisha Senoo

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

Related Questions