Hello. Tell me: There are intups of this type

<input type="text" name="name[]"> <input type="text" name="fabricator[]"> <input type="text" name="value[]"> 

This group of inputs will be specified in an indefinite number by the user, that is, the dynamics. So, the question is how to iterate over these arrays of all inputs and write to the database or how else to do better? So far, the current has done a brute force through one array, but is it possible to sort out everything at once and send it to the database?

 foreach($request->name as $key => $val ) { $param = Flavors::create(['name' => $val); } 
  • you ask what can be done better, but do not say what you need to do. In your case, as far as I understand, you just need a double nested loop. Although, the task is unclear, and perhaps you are doing something wrong. - Razzwan
  • The group of inputs will be in an unspecified quantity, the user will choose. Next, let's say with a click you need to collect all the data from the inputs and send it to the database. like this: the first group from the name [0] input, the fabricator [0], value [0] is one entry in the table, then if there is a name [1], the fabricator [1], value [1] then add as the second entry in the table and so on ... And can this be sent as a request? - axblue

1 answer 1

First, collect the data in one array, and then use insert Eloquent to insert it into the database.

 $data = []; $fabricator = $request->fabricator; $valueInput = $request->value; foreach($request->name as $key => $val ) { if(array_key_exists($key, $fabricator) && array_key_exists($key, $valueInput)){ $data[$key] = [ 'name' => $val, 'fabricator' => $request->fabricator[$key], 'value' => $request->value[$key] ]; } } if($data){ $param = Flavors::insert($data); }