Blade's $loop variable is interfering with my own code. I have my own $loop variable (which is an unrelated object), and whenever I call @foreach, Blade is overwriting my $loop. I know that I should 'suck it up', and name my variable something else. However, that would limit my code readability and force me to come up with a hacky workaround. I feel that Laravel should be more careful in terms of name collisions like this. Would it be possible to rename it to $_loop to avoid this happening?
Name any variable $loop prior to using @foreach or similar in a Blade template.
That would break everyones code, if they are already using $loop->foo. Just choose a different name.
I'm thinking this would be for a future release. Is there a way that I could potentially override it for myself?
I don't see why this is a bug. You are using a variable that blade requires it will for sure mess up with things.
This is not a bug, consider $loop as a reserved variable name just like $message is reserved for email views.
I'm not saying it's a bug, I'm just trying to come up with a namespacing for Laravel's reserved variables. This is way more widespread than just $loop. $message and $error also have this functionality. I understand why a change could complicate things, but a reserved variable name -- especially a name that is as common as these -- seems ridiculous to me.
It's a framework. It makes sense to have reserved variable names that are common.
The idea is to cover the 90% of the use cases.
If you want the variable to be prefixed by "laravel" or anything else, I'd advise you to close this issue and open a new one here: https://github.com/laravel/internals/issues
Thanks, @Belphemur. Will do!
Most helpful comment
It's a framework. It makes sense to have reserved variable names that are common.
The idea is to cover the 90% of the use cases.
If you want the variable to be prefixed by "laravel" or anything else, I'd advise you to close this issue and open a new one here: https://github.com/laravel/internals/issues