4

I have a foreach loop in a blade view, where I am listing the objects that have a pivot property price:

                @foreach($transaction->options as $option)
                  <div class="row">
                    <div class="col-md-6">
                      <p>Option description: {{ $option->description }}</p>
                      <p>Price: {{ $option->pivot->price }}</p>
                    </div>
                  </div>
                  <hr>
                @endforeach
                <div class="row">
                  <div class="col-md-6">
                    <h4>Options total: </h4>
                  </div>
                </div>

I would like to since I am doing a foreach loop here, calculate the sum of all the options so that I could write it next to Options total:

I have tried with this:

               @foreach($transaction->options as $option)
                  <div class="row">
                    <div class="col-md-6">
                      <p>Option description: {{ $option->description }}</p>
                      <p>Price: {{ $option->pivot->price }}</p>
                      @php($total += $option->pivot->price)
                    </div>
                  </div>
                  <hr>
                @endforeach
                <div class="row">
                  <div class="col-md-6">
                    <h4>Options total: {{ $total }}</h4>
                  </div>
                </div>

But, that didn't work, I got an error:

Undefined variable: total

How can I do this?

1
  • 6
    This is something you should be doing in your controller or domain logic and passing it to the view, it shouldn't be the view's job to calculate information. Commented Jan 18, 2018 at 15:41

4 Answers 4

25

Define the variable first:

@php($total = 0);

You can also get sum of pivot column with:

$transaction->options()->sum('price')
Sign up to request clarification or add additional context in comments.

Comments

5

Blade has a @php directive to execute a block of plain PHP within your template. Be aware that this may be a sign of bad design, and you should avoid put much logic inside your template. Ie:

@php
    $total = 0;
@endphp

@foreach ($items as $item)
<tr>
    <td>{{ $item->name }}</td>
    <td>{{ $item->code }}</td>
    <td>{{ $item->price }}</td>
</tr>

@php
    $total += $item->price;
@endphp

@endforeach

....

<p>Total {{ $total }}</p>

Comments

3

You can do that easily by saying

{{$transaction->options()->sum('price')}}

Comments

3

You can simply do the calculations in the blade template. This can be helpful for other users. Just do the calculation inside blade syntax {{ }} and use the display:none; css property. For example

<div style="display: none">
    {{ $total = 0 }}
</div>
@foreach($report as $device)
    <tr>
        <td>{{$device->id}}</td>
        <td>{{$device->price}}</td>
        <div style="display: none">{{$total += $device->price}}</div>
    </tr>
@endforeach
<tr>
    <th>Total Distance Travelled</th>
    <th>{{$total}}</th>
</tr>

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.