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 ?
Try to do this:
{{ $rs->${$item.'_data'} }}
http://php.net/manual/en/language.variables.variable.php
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With