Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

TimeSocks's avatar

Names of dynamically generated inputs

I have a form that dynamically generates inputs based on a user-given number:

@for($i = 0; $i < $userno['userno']; $i++)
            <div class="form-group">
                {!! Form::label('user[$i][name]','User Name:') !!}
                {!! Form::text('user[$i][name]', null, ['class' => 'form-control']) !!}
            </div>

            <div class="form-group">
                {!! Form::label('user[\{{ $i \}}][email]','User E-mail Address:') !!}
                {!! Form::text('user[\{{ $i \}}][email]', null, ['class' => 'form-control']) !!}
            </div>
@endfor

Here you can see two different methods I have tried to insert the iterator into the field name (I have added escaping slashes in the second input because the forum code seems to remove anything in double curly braces). Here is the output:

user: {
    $i: {
        name: "cxvzxcv"
    },
    <?php echo e($i); ?>: {
        email: "zxcvzx@zcxcx.com"
    }
},

Obviously neither will work when I try to retrieve the data. So how can I generate these fields and then retrieve their data in a Blade template?

TIA

Edit: I've managed to sort-of fix this by simply using standard HTML for the inputs rather than using the Form façade, but it would be nice to get it working with the façade if possible.

0 likes
2 replies
pmall's avatar
pmall
Best Answer
Level 56

Remove the braces. You are using blade braces in blade braces.

{!! Form::label('user[' . $i . '][email]','User E-mail Address:') !!}
{!! Form::text('user[' . $i . '][email]', null, ['class' => 'form-control']) !!}

What is between blade braces is plain php code, you made a mistake trying to output a var with blade braces.

Please or to participate in this conversation.