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

leandroEpifani's avatar

Access to request element that start with the same name

Hi to all, I have a piece of code that create new form field with the last part of the name that have an id different every time that i create a new form field. My question is how can i access to this field from $request if i know only the first part of the field name?

{{Form::text('name-1','',array('class'=>'form-control'))}} {{Form::text('name-2','',array('class'=>'form-control'))}} {{Form::text('name-4','',array('class'=>'form-control'))}} {{Form::text('name-223','',array('class'=>'form-control'))}}

the number after the name can change a lot and i need to get the name of the fieldName to make a foreach.

Anyone has a solution? Thanks a lot.

0 likes
2 replies
topvillas's avatar

Can't you just use the PHP array syntax?

{{Form::text('name[],'',array('class'=>'form-control'))}}

You'll then be able to use the posted input name as an array.

Cronix's avatar

Yes, use array syntax. You can just put your "id" or whatever it is as the array key and then access it in the controller

{{Form::text('name[1]','',array('class'=>'form-control'))}}
{{Form::text('name[2]','',array('class'=>'form-control'))}}
{{Form::text('name[4]','',array('class'=>'form-control'))}}
{{Form::text('name[223]','',array('class'=>'form-control'))}}
foreach ($request->name as $key => $value) {
    //$key = 1, 2, 4, then 223, etc.
}

Please or to participate in this conversation.