Foreach loop in HTML Form Builder form

Published 10 months ago by JokersMild85

I wish I could more eloquently explain what I am trying to accomplish here but I am still quite new to Laravel so please forgive if I misuse any terminology in my explanation.

I am trying to apply a foreach loop to my form that I built using the Laravel Collective HTML Form Builder (...correct?) and apply the values I pull from the database to the value fields of the form.

The output it is giving me is what is literally typed in my code. I know its because of the single quotation marks but if I remove them I get a syntax error for not including them.

Here is my code below as it will probably explain what I am trying to do better than how I am describing it.

The controller create function:

public function create()
{
    //
    $exercise_types = ExerciseType::all();
    
    return view('workouts.create', ['exercise_types' => $exercise_types]);
}

The create view:

@extends('layouts.app')

@section('content')

Create New Workout

{!! Form::open(['url' => 'workouts']) !!}

@include ('workouts.form', ['submitButtonText' => 'Create Workout'])

{!! Form::close() !!}

@endsection

The form view itself:

@foreach($exercise_types as $exercise_type)

{!! Form::label('exercise_type', '{{ $exercise_type->exercise_type }}:') !!}
@foreach($exercise_type->exercises as $exercise)

    {!! Form::checkbox( '{{ $exercise->exercise_name }}', '{{ $exercise->id }}-{{ $exercise->exercise_type_id }}-{{ $exercise->exercise_name }}') !!}

@endforeach

@endforeach

{!! Form::submit($submitButtonText, ['class' => 'btn btn-primary form-control']) !!}

Any guidance would be appreciated.

Best Answer (As Selected By JokersMild85)
tykus

You should not need the Blade braces within the Blade braces:

{!! Form::checkbox( $exercise->exercise_name, "{$exercise->id}-{$exercise->exercise_type_id}-{$exercise->exercise_name}") !!}

For the second parameter above, the braces are for string interpolation only.

tykus
tykus
10 months ago (658,320 XP)

You should not need the Blade braces within the Blade braces:

{!! Form::checkbox( $exercise->exercise_name, "{$exercise->id}-{$exercise->exercise_type_id}-{$exercise->exercise_name}") !!}

For the second parameter above, the braces are for string interpolation only.

JokersMild85

That did it, thanks. Is it possible to string those multiple variables together in the second parameter?

tykus
tykus
10 months ago (658,320 XP)

What do you mean by string those multiple variables together; this precisely is what the string interpolation has done?

JokersMild85

Gotcha, didn't realize that's what you were doing. I appreciate the help, thank you.

tykus
tykus
10 months ago (658,320 XP)

Cool. Mark the correct answer to help others too.

jlrdw
jlrdw
10 months ago (244,950 XP)

Yes encapsulate the answer.

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