Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenation with Blade in Laravel

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 ?

like image 566
ProEvilz Avatar asked Mar 31 '16 15:03

ProEvilz


1 Answers

Try to do this:

{{ $rs->${$item.'_data'} }}

http://php.net/manual/en/language.variables.variable.php

like image 193
Alexey Mezenin Avatar answered Sep 20 '22 14:09

Alexey Mezenin