Home > Enterprise >  Increase variable in column name Laravel
Increase variable in column name Laravel

Time:10-01

I have those columns in my database table :

value_day_1 | value_day_2| value_day_3 |......|value_day_36

I'm trying to display each value in a view using a for loop

@for ($n=1;$n<37;n  )
     {{ $day->value_day_? }}
@endfor

How can i replace the ? by $n ?

CodePudding user response:

One solution would be

@foreach(range(1,37) as $n)
    @php($column = 'value_day_' . $n;)
    {{ $day->$column }}
@endforeach

I prefer to use range instead of the for syntax but it is not neccessary for your problem

CodePudding user response:

@for ($n=1;$n<37;n  ) 
    $d='value_day_'.$n;
   {{ $day->$d }}
@endfor  

Just assign to a new variable before

CodePudding user response:

You can do this easily inline:

$day->{'value_day_'. $n}
  • Related