0

I'd like to create an array of objects (or a bi-dimensional array) from a form using Laravel

the blade.php looks like

@foreach($objets as $objet)
<tr>
    <td>
        <input type="text" name="objets[]" id="field_A" value="{{$objet->field_A}}">
    </td>
    <td>
        <input type="text" name="objets[]" id="field_B" value="{{$objet->field_B}}">
    </td>
</tr>
@endforeach

Currently, when validating the form, the variable $objets is an array, with all values, but not an array of array.

1
  • Paste the code for $objets you can cast into an object for your convenience Commented Jun 11, 2019 at 16:31

1 Answer 1

1

Update your input names as follows:

<td>
  <input type="text" name="objets[][fieldA]" id="field_A" value="{{$objet->field_B}}">
</td>
<td>
  <input type="text" name="objets[][fieldB]" id="field_B" value="{{$objet->field_B}}">
</td>

The above will output an array with array

"objects" => array:1 [▼
                0 => array:7 [▼
                   'fieldA' => 'value'
                   'fieldB' => 'value'
                ]
             ]

Then in your validation you can use it like this:

$validate['objects.*.field_A'] = 'required';
$validate['objects.*.field_B'] = 'required';
return $validate;
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.