Home:ALL Converter>Laravel foreach variable loop scope

Laravel foreach variable loop scope

Ask Time:2018-02-26T19:12:05         Author:Mayur Panchal

Json Formatter

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 is accessible outside endforeach loop after @endforeach

Author:Mayur Panchal,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/48987159/laravel-foreach-variable-loop-scope
Alexey Mezenin :

From the foreach docs:\n\nWarning\nReference of a $value and the last array element remain even after the foreach loop. It is recommended to destroy it by unset()\n\nSo, if you want to destroy the reference, do this:\n<?php unset($ref); ?>\n\nOr:\n@php unset($ref); @endphp\n",
2018-02-26T11:41:29
Maitri Patel :

It is recommended to destroy it by unset()\n\n @php unset($ref); @endphp\n",
2018-12-18T07:17:20
yy