When I use Laravel blade file foreach loop, the variable is accessible after the foreach loop, whereas the scope of the variable should be only within the loop
@foreach($user->referral as $ref)
<tr>
<td>{{ $ref->referral_amount }}</td>
<td>{{ $ref->status }}</td>
</tr>
@endforeach
$ref: This variable accessible outside endforeach loop after @endforeach
From the foreach
docs:
Warning
Reference of a
$value
and the last array element remain even after theforeach
loop. It is recommended to destroy it byunset()
So, if you want to destroy the reference, do this:
<?php unset($ref); ?>
Or:
@php unset($ref); @endphp
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With