A
A
axblue2016-10-08 17:42:27
Laravel
axblue, 2016-10-08 17:42:27

How to loop through an array of inputs?

Hello. Prompt: There are inputs of this type

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

This group of inputs can be set in an indefinite number by the user, that is, the dynamics. So, the question is, how to iterate through these arrays and write them to the database, or how else to do it better?
So far, the current has searched through one array, but is it possible to sort through everything at once?
foreach($request->name as $key => $val ) {
            $param = Flavors::create(['name' => $val); 
        }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Tesla, 2016-10-09
@leshikgo

To add multiple records in one query use Query builder insert()
Something like this:

$flavours = collect($request->name)->map(function ($value) {
  return ['name' => $value];
})->all();

DB::table('flavours')->insert($flavours);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question