skip to Main Content

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 ?

3

Answers


  1. 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

    Login or Signup to reply.
  2. @for ($n=1;$n<37;n++) 
        $d='value_day_'.$n;
       {{ $day->$d }}
    @endfor  
    

    Just assign to a new variable before

    Login or Signup to reply.
  3. You can do this easily inline:

    $day->{'value_day_'. $n}
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search