Matthew
Matthew

Reputation: 1655

Counting returned true if statements in Laravel

I have a Blade with a rather long (at least for me) set of conditional statements. At the moment it looks like this:

    <table>
    <tbody>
        @foreach($payments as $payment)

    <tr>
    <td style="width:120px;">{{$payment->id}}</td>
<td>@if($payment->payer_id){{$payment->payer->customer_name}}@endif</td>
<td style="width:120px;">{{$payment->payment_amount}}</td><td>{{$payment->payment_distributions->count()}}
        @if($payment->payment_distributions->count()>0)
            @foreach($payment->payment_distributions as $pd)
                @if($pd->amount > 0)
                    @if($pd->shipment->balance)
                        @if($pd->amount < $pd->shipment->balance)
                             <small class="label pull-right bg-red">1</small>
                        @else
                        @endif



                    @else
                    @endif

                @else

                @endif
            @endforeach

            @else
        @endif

        </td>
    </tr>
        @endforeach
    </tbody>    
    </table>

In the middle of all that is where it is important, as you can see, it returns a red 1 if the innermost statement returns true. This is of course solely for my benefit, but what I would like is to have it count how many times within overall if statement it returns true, so rather than returning 7 red 1s, I'd like it to return just a red 7.

Upvotes: 2

Views: 1584

Answers (3)

Binny Chanchal
Binny Chanchal

Reputation: 36

Do this :

<table>
<tbody>
    @foreach($payments as $payment)
        @php
            $customer_name  =  ($payment->payer_id) ?  $payment->payer->customer_name : "";
            $filtered_payment_distributions =  $payment->payment_distributions->each(function ($pd, $key) {
                if(($pd->amount > 0) && ($pd->shipment->balance) && ($pd->amount < $pd->shipment->balance)){
                    return $pd;
                }
            });
        @endphp
        <tr>
        <td style="width:120px;">{{$payment->id}}</td>
        <td>{{$customer_name}}</td>
        <td style="width:120px;">{{$payment->payment_amount}}</td>
        <td>
            {{$payment->payment_distributions->count()}}
            @if($filtered_payment_distributions->count() > 0)
                <small class="label pull-right bg-red">{{$filtered_payment_distributions->count()}}</small>
            @endif
        </td>
        </tr>
    @endforeach
</tbody>    

Upvotes: 0

Alexey Mezenin
Alexey Mezenin

Reputation: 163768

Do this:

@php($counter = 0)
@foreach($payment->payment_distributions as $pd)
    @if($pd->amount > 0 && $pd->shipment->balance && $pd->amount < $pd->shipment->balance)
        @php($counter++)
    @endif
@endforeach
<small class="label pull-right bg-red">{{ $counter }}</small>

Instead of this:

@foreach($payment->payment_distributions as $pd)
    @if($pd->amount > 0)
        @if($pd->shipment->balance)
            @if($pd->amount < $pd->shipment->balance)
                 <small class="label pull-right bg-red">1</small>
            @else
            @endif



        @else
        @endif

    @else

    @endif
@endforeach

Upvotes: 4

Deepak Kumar T P
Deepak Kumar T P

Reputation: 1076

@php
    $count=0;
@endphp
@foreach($payment->payment_distributions as $pd)
    @if($pd->amount > 0)
        @if($pd->shipment->balance)
            @if($pd->amount < $pd->shipment->balance)
                @php
                    $count++;
                @endphp
            @else
            @endif
            @else
        @endif
    @else
    @endif
@endforeach
<small class="label pull-right bg-red">{{$count}}</small>

Upvotes: 0

Related Questions