Home:ALL Converter>Laravel Blade Variable Variable

Laravel Blade Variable Variable

Ask Time:2018-05-16T00:19:48         Author:user908759

Json Formatter

With Laravel 5.4, I am using a variable variable ($$var) in a blade template to access a model attribute. All the attributes work except the description attribute. Below is an example:

Model Migration:

Schema::create('user_roles', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name', 50);
    $table->string('description', 255);
    $table->timestamps();
});

Controller:

...
$tableCols = [
    ['name' => 'id'],
    ['name' => 'name'],
    ['name' => 'description']
]
...

Blade NOT WORKING:

@foreach($tableCols as $tableCol)
    {{ $name = $tableCol['name']}}
    <td>{{ $model->$$name }} </td>
@endforeach

Error: Undefined variable: description

Blade WORKING:

<td> {{ $model->name }} </td>
<td> {{ $model->description }} </td>

How do I get my variable variable $$ to get the description attribute. Thanks!

Author:user908759,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/50354959/laravel-blade-variable-variable
yy