Framework: Laravel foreach in blade template overiding variables

Created on 25 Sep 2017  路  2Comments  路  Source: laravel/framework

Description:

Laravel foreach in blade template overiding variables.

It changes the variable $user after the name has been used in foreach block, while this is not in pure PHP and other systems.

{{ $user['username'] }}   

@foreach ($followers as $user)
      {{$user['username']}}
@endforeach

{{ $user['username'] }}

Steps To Reproduce:

1, Define a array called $followers and a $user

      $data = [
            "user"=>["username"=>"original_name"]
            "followers" => [
                    ["username"=>"new_name"]
            ]
      ];

2, Paint it to template

Most helpful comment

PHP foreach (which is used under the hood) has the same behaviour. You must use another name for foreach variable.

All 2 comments

PHP foreach (which is used under the hood) has the same behaviour. You must use another name for foreach variable.

Yes, my fault. And I hate this behavior. 馃槶

Was this page helpful?
0 / 5 - 0 ratings

Related issues

RomainSauvaire picture RomainSauvaire  路  3Comments

kerbylav picture kerbylav  路  3Comments

JamborJan picture JamborJan  路  3Comments

shopblocks picture shopblocks  路  3Comments

iivanov2 picture iivanov2  路  3Comments