Blade Concatenation in Laravel

I am trying to access a table column in an ItemController. In this case, I want to use the values ​​in my array + the concatenated string 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

Required Conclusion:

$rs->Alpha_data;
$rs->Delta_data;
etc

How can I dynamically set a variable for $ rs → name ?

+6
source share
3 answers

Try to do this:

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

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

+12
source

, 'task', 'var1', 'var2', 'var3'... 'var16'.

:

@for($y=1; $y<=16; $y++)
 {{ $task->{'var'.$y} }}
@endfor
0

Pretty simple, just use it. sign up for concats in php

-1
source

All Articles