Why $variable++ not working properly in Lavarel Blade?

82 Views Asked by At

I just encounter a situation where I need to display index for each row of table, For this I created a variable ($number) in blade view file and set it to 0. For row of my table I created a component and pass my data to it, incrementing index ($number) for each row. But actually it is incrementing twice.

My blade :

@if($staff_profile->passport_expiry!=null&&$staff_profile->passport_expiry<$exp_date)
    <x-staf_expiry_row :id=" $staff_profile->id" :name="$staff_profile->name" type="Passport" :date="$staff_profile->passport_expiry" :number="$number++"/>
@endif

and the result is:

before

But it works fine after a simple change :

@if($staff_profile->passport_expiry!=null&&$staff_profile->passport_expiry<$exp_date)
    <x-staf_expiry_row :id=" $staff_profile->id" :name="$staff_profile->name" type="Passport" :date="$staff_profile->passport_expiry" :number="$number++"/>
    @php($number++)
@endif

after

Why $number++ increment twice and what's the proper way of doing it?

Thanks for your time!


EDIT: my table body in blade:

<tbody>
     @foreach($staff_profiles as $key => $staff_profile)
        @if ($staff_profile->passport_expiry != null && $staff_profile->passport_expiry < $exp_date)
            <x-staf_expiry_row :id=" $staff_profile->id" :name="$staff_profile->name" type="Passport" :date="$staff_profile->passport_expiry" :number="$number++"/>                       
        @endif
        @if ($staff_profile->visa_expiry != null && $staff_profile->visa_expiry < $exp_date)
            <x-staf_expiry_row :id=" $staff_profile->id" :name="$staff_profile->name" type="Visa" :date="$staff_profile->visa_expiry" :number="$number++"/>                       
        @endif
        @if ($staff_profile->emirates_id_expiry != null && $staff_profile->emirates_id_expiry < $exp_date)
            <x-staf_expiry_row :id=" $staff_profile->id" :name="$staff_profile->name" type="Emirates Id Expiry" :date="$staff_profile->emirates_id_expiry" :number="$number++"/>                       
        @endif
        @if ($staff_profile->labor_card_expiry != null && $staff_profile->labor_card_expiry < $exp_date)
            <x-staf_expiry_row :id=" $staff_profile->id" :name="$staff_profile->name" type="Labor Card Expiry" :date="$staff_profile->labor_card_expiry" :number="$number++"/>                       
        @endif
        @if ($staff_profile->contract_expiry != null && $staff_profile->contract_expiry < $exp_date)
            <x-staf_expiry_row :id=" $staff_profile->id" :name="$staff_profile->name" type="Contract Expiry" :date="$staff_profile->contract_expiry" :number="$number++"/>                       
        @endif
    @endforeach
</tbody>
1

There are 1 best solutions below

4
matiaslauriti On

Use the $loop variable from @foreach, so, let's say you have a @foreach, you should have this blade:

@foreach ($staff_profiles as $staff_profile)
    @if($staff_profile->passport_expiry != null && $staff_profile->passport_expiry < $exp_date)
       <x-staf_expiry_row :id="$staff_profile->id" :name="$staff_profile->name" type="Passport" :date="$staff_profile->passport_expiry" :number="$loop->index"/>
    @endif
@endforeach