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

ghost picture ghost  路  3Comments

JamborJan picture JamborJan  路  3Comments

lzp819739483 picture lzp819739483  路  3Comments

Anahkiasen picture Anahkiasen  路  3Comments

SachinAgarwal1337 picture SachinAgarwal1337  路  3Comments