0

I have display quantity and price attributes on my table from database but for some reasons I don't save the total into database but I display total by multiplying quantity * total on the table when data is being fetched.

Total for every column on my table is being displayed accurately. Is there a way that I can add all total columns on the table in my html?

PS: with my code, it only displays the total of the current column of the table

Table

<tbody>
    @foreach($items as $items)
        <tr>
            <td>{{$item>id }}</td>
            <td>{{$item->quantity}}</td> 
            <td>{{ $item->price}}</td>
            <td>{{ $item->quantity * $item->price}}</td>
        </tr>    
    @endforeach
    <p>Sum: {{ $item->quantity * $item->price}}</p>
</tbody>
1
  • you can easily google this kind of things do not post this kind of questions here. you can use JS as well jsfiddle.net/z_acharki/jnwrc5ay/347 Commented Nov 13, 2017 at 11:54

4 Answers 4

1
<tbody>
    {{ $total = 0 }}
    @foreach($items as $items)
        <tr>
            <td>{{$item>id }}</td>
            <td>{{$item->quantity}}</td> 
            <td>{{ $item->price}}</td>
            <td>{{ $item->quantity * $item->price}}</td>
            {{ $total = $total + ($item->quantity * $item->price) }}
        </tr>

    @endforeach
    <p>Sum: {{ $total }}</p>
</tbody>
Sign up to request clarification or add additional context in comments.

3 Comments

don't do copy paste you copied sapnesh ans. and just lil bit modified it.
Yeah dude, look at the time difference, < 1min, also his code doesn't work.
yup ! you are correct let me give you +1 for this :-)
0

You can use blade's @php directive like this

<tbody>
@php
$total = 0
    @foreach($items as $items)
    <tr>
        <td>{{$item>id }}</td>
        <td>{{$item->quantity}}</td> 
        <td>{{$item->price}}</td>
        <td>{{$total = $total + ($item->quantity * $item->price)}}</td>        
    </tr>        
    @endforeach

    <p>Sum: {{ $total }}</p>
@endphp
</tbody>

Comments

0

Try the following:

{{ $sum = 0 }}
<tbody>
    @foreach($items as $items)
       <tr>
          <td>{{$item>id }}</td>
          <td>{{$item->quantity}}</td> 
          <td>{{ $item->price}}</td>
          <td>{{ $item->quantity * $item->price}}</td>
      </tr>
      $sum+ = $item->quantity * $item->price;
    @endforeach
    <p>Sum: {{ $sum }}</p>
</tbody>

Comments

0

I assume here $items is a Illuminate\Support\Collection. You can calculate a sum using a closure:

$items->sum( function ($item) {
    return $item->quantity * $item->price;
});

Or in your blade template:

{{ $items->sum( function ($item) {
    return $item->quantity * $item->price;
}); }}

See Laravel Collections docs.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.