0

I have the following array:

$days = [
    ['dow' => 1, 'day' => 'Monday'],
    ['dow' => 2, 'day' => 'Tuesday'],
    ['dow' => 3, 'day' => 'Wednesday'],
    ['dow' => 4, 'day' => 'Thursday'],
    ['dow' => 5, 'day' => 'Friday'],
    ['dow' => 6, 'day' => 'Saturday'],
    ['dow' => 7, 'day' => 'Sunday'],
];

I'm trying to loop through it in my blade template as follows:

 @foreach ($days as $day)
       <label>{{$day['day']}}</label>
       {!! Form::text('day_of_wk[$day['dow']].start_time', null, ['class' => 'form-control'])!!}
  @endforeach

But I get error: syntax error, unexpected 'dow' (T_STRING)

How can I add the $day['dow'] value in my day_of_wk[] input array?

5
  • Take a look at the quotes around them. There is an syntax error. Remove the single Quotes or replace them by double quotes. Commented Jan 9, 2016 at 20:59
  • already tried that, it doesn't work. I think you can't add a variable in this manner when using laravel collective, unless someone can show me otherwise. Commented Jan 9, 2016 at 21:07
  • When I remove the single quotes I just get $day[dow] instead of the actual value Commented Jan 9, 2016 at 21:12
  • Managed to fix by doing the following 'day_of_wk['. $day['dow'] . '].start_time' Commented Jan 9, 2016 at 21:14
  • You can embed variables in php strings only when using double quotes. With single quotes you need to concatenate it as you found above. Commented Jan 10, 2016 at 3:25

2 Answers 2

7
  @foreach ($days as $day)
       <label>{{$day['day']}}</label>
       {!! Form::text('day_of_wk['.$day['dow'].'].start_time', null, ['class' => 'form-control'])!!}
  @endforeach

or

@foreach ($days as $day)
           <label>{{$day['day']}}</label>
           {!! Form::text("day_of_wk[{$day['dow']}].start_time", null, ['class' => 'form-control'])!!}
      @endforeach
Sign up to request clarification or add additional context in comments.

Comments

-1

@foreach ($days as $day) {{$day['day']}} {!! Form::text("day_of_wk[{$day['dow']}].start_time", null, ['class' => 'form-control'])!!} @endforeach

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.