Laravel foreach with a variable path

When I use the foreach loop for the blade of a Laravel file, the variable is available after the foreach loop, while the scope of the variable should only be inside the loop

@foreach($user->referral as $ref) <tr> <td>{{ $ref->referral_amount }}</td> <td>{{ $ref->status }}</td> </tr> @endforeach 

$ ref: this variable is available outside the endforeach loop after @endforeach

+8
source share
2 answers

From foreach docs :

Attention

The reference a $value and the last element of the array remain even after the foreach . Recommended to destroy it unset()

So, if you want to destroy the link, do the following:

 <?php unset($ref); ?> 

Or:

 @php unset($ref); @endphp 
+6
source

It is recommended to destroy it with unset ()

  @php unset($ref); @endphp 
+2
source

Source: https://habr.com/ru/post/1275581/


All Articles