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

Sushant97's avatar

Laravel 5.8: how to perform query building for bulk data from a form

I have a form which contains a table that has 3 editable fields, lets call them A,B,C. The table's number of rows is variable, and so the number of A,B,C attribute values is not fixed.

For example, if there are 5 rows, there will be five A, five B and five C values, which is 15 values in total:

|A  |B  |C |
------------
|0  |1  |2  |
|3  |4  |5  |
|6  |7  |8  |
|9  |10 |11 |
|12 |13 |14 |

Right now, the naming of these editable fields is a problem. I'm doing it dynamically, so that I can access each value individually like:

<?php $i = 0; ?>
@foreach($data as $row)
     <?php 
        $i++;
        $ia1 = "ia1_".$i;
        $ia2 = "ia2_".$i;
        $ia3 = "ia3_".$i; 
    ?>
    <tr>
    <td><input name ="{{ $ia1 }}" type="text" value="{{ $row->IA1 }}" style="width:100%;text-align:center;"> 
    </input></td>
    <td><input name ="{{ $ia2 }}" type="text" value="{{ $row->IA2 }}" style="width:100%;text-align:center;"> 
    </input></td>
    <td><input name ="{{ $ia3 }}" type="text" value="{{ $row->IA3 }}" style="width:100%;text-align:center;"> 
    </input></td>
    </tr>
@endforeach

and so, I am getting the values through the post array as:

 [
  "_token" => "*token value here*"
  "A_1" => "0"
  "B_1" => "1"
  "C_1" => "2"
  "A_2" => "3"
  "B_2" => "4"
  "C_2" => "5"
  "A_3" => "6"
  "B_3" => "7"
  "C_3" => "8"
  "A_4" => "9"
  "B_4" => "10"
  "C_4" => "11"
  "A_5" => "12"
  "B_5" => "13"
  "C_5" => "14"
]

My database too has a table with the same 3 fields. How do I update these 5 rows in that table? How do I access the A,B,C values for individual row (For example, A_2, B_2, C_2), and use them in my update query?

0 likes
1 reply
ftiersch's avatar

I wouldn't use naming like that, I'd use array naming.

<input name="a_data[{{ $i }}]" type="text" value="{{ $row->IA1 }}">

Then you get an array of values for a, b and c and can manipulate the array however you like.

Also you don't need your $i variable. You can use the $loop variable inside a @foreach to access the current index :)

So I'd actually do this:

<input name="a_data[{{ $loop->index }}]" type="text" value="{{ $row->IA1 }}">

Please or to participate in this conversation.