mardi 24 septembre 2019

How to insert multiple array with sub array as rows in Mysql using Laravel

I have a table with following field id,name, type, color

I have following input fields and want to add multiple rows

<input type="text" name="com_name[]" value="a">
<input type="text" name="com_type[]" value="1">
<input type="text" name="com_color[]" value="red">
<input type="text" name="com_type[]" value="2">
<input type="text" name="com_color[]" value="green">

<input type="text" name="com_name[]" value="b">
<input type="text" name="com_type[]" value="1">
<input type="text" name="com_color[]" value="black">
<input type="text" name="com_type[]" value="2">
<input type="text" name="com_color[]" value="yellow">

I am trying laravel but if some can show how to do it in core PHP that would also be great help

$com_name = array();
$com_name = Input::get('com_name');
$dataSet = [];
foreach ($com_name as $name) {
    $dataSet[] = [
        'name'  => $request->com_name,
        'type'    => $request->com_type,
        'color'       => $request->com_color,
    ];
}

DB::table('extra')->insert($dataSet);

I want the following output in my mysql table

ID  Name  Type  Color
1    a     1     red
2    a     2     green
3    b     1     black
4    b     2     yellow


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire