0

I am trying to create a form where the names of the inputs are an array. I am going through an array sent from another view to get fields to show. in this example I want to show 3 fields.

$someResult = array('0','1','2');


$fields = array(0=>'fName',1=>'mName',2=>'lName');

@foreach($someResult as $k){

     <td> {!! Form::text($fields[$k][],$someVal) !!}</td>

@endforeach

I tried simplifing it to straight php:

  foreach ($someResult as $k){
    echo "<tr><td><input type='text' name='".$fields[$k][]."' value='".$someVal."'></tD></tr>";
    }

Either way, i get the error "Cannot use [] for reading"

How can i declare name argument as an array?

My goal is something like:

   <td>
       <input type='text' name='fName[]' value='someVal'>
   </td>
   <td>
       <input type='text' name='mName[]' value='someVal'>
  </td>
  //etc.....
1
  • this is wrong Form::text($fields[$k][],$someVal) specifically $fields[$k][] That last [] should be part of the name array $fields = array(0=>'fName[]',1=>'mName[]',2=>'lName[]'); etc. Or as part of the "string" name, you could concatenate it etc... later of course Form::text($fields[$k].'[]',$someVal) Commented Feb 26, 2019 at 18:46

1 Answer 1

1

The square bracket should be used as a string in your dom.

@foreach ($someResult as $k)
    <tr>
        <td>
            <input type="text" name="{{ $fields[$k] }}[]" value="{{ $someVal }}">
        </td>
    </tr>
@endforeach
Sign up to request clarification or add additional context in comments.

Comments

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.