jsrosas
2 years ago

Set a variable name out of 2 other variables inside a for loop

Posted 2 years ago by jsrosas

Hi all, I have a database not designed by me which has some values as month1, month2, month3 etc etc until month24. I need to display each value and I am looking for a quicker way other than manually doing 24 HTML lines. Because for each of this values echoing in Blade I have to do if, ifelse checks. I was hoping I could do a for loop and just concatenate the variable name with the index of the for loop to the the month1 until month24

@for($i =1; $i< 25; $i++)
              @php
               echo $acc->history()[1]->month{{$i}};
              @endphp
 @endfor

And I keep getting Parse error: syntax error, unexpected '?'. I tried doing:

@{{$acc->history()[1]->month.$i}}

But that just simply echoes the desired variable name as a string. Any ideas how to solve this? Or a better way to iterate through the fields?

Please sign in or create an account to participate in this conversation.