Reputation: 5455
I'm trying to access a table column in my ItemController. In this instance, I wish to use the values in my array + a concatenated string to for the column name.
ItemController.php ....
public function displayItems() {
$itemsList = array('Alpha','Bravo','Charlie','Delta');
//$results = returned mysql row here
return view('items', ['rs' => $results, 'items' => $itemsList]);
}
page.blade.php
@foreach ($items as $item)
//$item is used elsewhere too, so keep $item
{{$rs->$item.'_data'}}
@endforeach
Desired output:
$rs->Alpha_data;
$rs->Delta_data;
etc
How can I dynamically set a variable for $rs->name ?
Upvotes: 15
Views: 66883
Reputation: 1431
lets say you are retrieving a single record 'task', and this record has variables called 'var1','var2','var3' ... all the way to 'var16'.
you can echo those variables in your blade file like so:
@for($y=1; $y<=16; $y++)
{{ $task->{'var'.$y} }}
@endfor
Upvotes: 3
Reputation: 163768
Try to do this:
{{ $rs->${$item.'_data'} }}
http://php.net/manual/en/language.variables.variable.php
Upvotes: 25