1

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>
matiaslauriti
  • 7,065
  • 4
  • 31
  • 43
Usama Jalal
  • 105
  • 1
  • 8

1 Answers1

0

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
matiaslauriti
  • 7,065
  • 4
  • 31
  • 43
  • actually I cant, as in for each i have 6 if condition, which will generate new row , and index has to be different. My main concern was why blade is incrementing twice. thanks BTW for your help :) – Usama Jalal Aug 21 '22 at 17:33
  • @UsamaJalal could you share the whole file then? – matiaslauriti Aug 22 '22 at 15:52
  • edited, have a look – Usama Jalal Aug 22 '22 at 16:28
  • @UsamaJalal I saw the update, so my question would be, what are you using `$number` for? Why do you need to increment it? Depending on your answer, I could say, you may pass the property name and the `$loop->index` so you end up having `$array[$loop->index] = ['Passport' => xxxx. 'Visa' => xxxx];` but I am not sure how you are using `$number` and what for – matiaslauriti Aug 22 '22 at 22:51