rename your form entries for easier management later
<td><input type="email" name="rows[{{ $row->id }}][email]"></td>
<td><input type="text" name="rows[{{ $row->id }}][username]"></td>
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hello,
In the frontend I have a form wrapping a table with the name attributes being arrays (with the ids from the DB being the keys):
<form>
<table>
@foreach($rows as $row)
<tr>
<td><input type="email" name="email[{{$row->id}}]"></td>
<td><input type="text" name="username[{{$row->id}}]"></td>
</tr>
@endforeach
</table>
</form>
When the form data is sent to the backend, it is grouped by the name attributes:
[
"email" => [11 => "[email protected]", 332 => "[email protected]"],
"username" => [11 => "foo", 332 => "bar", 404 => "baz"],
]
I need to update the DB where the id in the DB equals the id from the input. What I do is to first "regroup" the input by the id via PHP, so the end result is:
$data_to_update = [
11 => ["email" => "[email protected]", "username" => "foo"],
332 => ["email" => "[email protected]", "username" => "bar"],
404 => ["username" => "baz"],
]
Then I can iterate each and update:
foreach ($data_to_update as $id => $data) {
DB::table('users')
->where('id', $id)
->update($data);
}
But is there a way to skip the PHP regrouping part and do it on the original structure (Without extra queries)?
Thanks
rename your form entries for easier management later
<td><input type="email" name="rows[{{ $row->id }}][email]"></td>
<td><input type="text" name="rows[{{ $row->id }}][username]"></td>
Please or to participate in this conversation.