PHP How to save data from an array in mysql using laravel 5

Im gets an array from the Cylinders data array from POST:

Array ( [serie] => Array ( [0] => 1234 [1] => 3545 ) [seriesap] => Array ( [0] => 1234234 [1] => 345345 ) [type] => Array ( [0] => 4546 [1] => csdfwe ) [admission] => Array ( [0] => 04-05-2015 [1] => 04-05-2015 ) [invoice] => Array ( [0] => fei76867 [1] => feiasodjf ) ) 

Now the fields inside the keys: serial, type, input, etc. do not change, but the information inside these keys changes, I mean that there can even be 15 elements.

In the end I need to save in the database:

 $cylinder = new Cylinder(); $cylinder->serie = ??; $cylinder->seriesap = ??; $cylinder->type = ??; $cylinder->admission = ??; $cylinder->invoice = ??; $cylinder->save 

How can I complete this task and save all the cylinders?

I tried everything that I can think of, nothing seems to work.

/edit/

This is what I am doing so far:

 $cyldata = $_POST['cylinder']; //this is the post from top. $num_elements = 0; while($num_elements < count($cyldata['serie'])){ $cylinder = new Cylinder(); $cylinder->serie = $cyldata['serie'][$num_elements]; $cylinder->type = $cyldata['type'][$num_elements]; $cylinder->admission = $cyldata['admission'][$num_elements]; $cylinder->seriesap = $cyldata['seriesap'][$num_elements]; $cylinder->save $num_elements++; } 

But he feels ugly, all these savings do not feel good. A dirty decision if you ask me.

+6
source share
3 answers

First you need to convert the input to a different format:

 $cyldata = $_POST['cylinder']; //this is the post from top. $num_elements = 0; $sqlData = array(); while($num_elements < count($cyldata['serie'])){ $sqlData[] = array( 'serie' => $cyldata['serie'][$num_elements], 'type' => $cyldata['type'][$num_elements], 'admission' => $cyldata['admission'][$num_elements], 'seriesap' => $cyldata['seriesap'][$num_elements], 'invoice' => $cyldata['invoice'][$num_elements], // you miss this field, aren't you? 'created_at' => Carbon\Carbon::now(), // only if your table has this column 'updated_at' => Carbon\Carbon::now(), // only if your table has this column ); $num_elements++; } 

Secondly, use the Fluent query designer to create an insert into the package:

 DB::table('table_name')->insert($sqlData); 

Note. created_at and updated_at displayed here if your table has this field. When working with the Eloquent model, this field is updated automatically. However, we do not use Eloquent, so we need to assign a value for this field manually.

+6
source

Since you have an array to insert data into the database, you can try the create method from the model:

 Cylinder::create($array); 

but actually the key array requires field_name in your database. Or you can do this using the query builder:

 DB::table('table_name')->insert($array); 

and again you need to set the key array as field_name in your database.

+1
source

I do not know what made you use the $_POST global array to receive data from the user.

Perhaps this is what you want.

 /** * Store the form inputs in the table * * @param Request $request */ public function store( Request $request ) { $data = Input::get(); for($i = 0; $i < count($data['serie']); $i++) { $c = new Cylinder(); $c->serie = $data['serie'][$i]; $c->type = $data['type'][$i]; $c->admission = $data['admission'][$i]; $c->seriesap = $data['seriesap'][$i]; $c->save(); // fixed typo } } 
+1
source

Source: https://habr.com/ru/post/988058/


All Articles