Home > Mobile >  How to save multiple array input data in Database in Laravel PHP
How to save multiple array input data in Database in Laravel PHP

Time:06-02

Hello Geeks i am stuck here. I am beginner and want to know how to save and update array data in database. this is laravel form requested data

I want to save data like this : mysql database

here is my form request, i want to save data in mysql database, with new row :

"_token" => "FoyoYvFXmNAggbQuMq6PN243QS43MkY99Nq3UAni"
"description" => array:2 [▼
 0 => "<h3>Shared Hosting Plan</h3>"
 1 => "<h3>Cloud Hosting Plan</h3>"
]
"site_id" => array:2 [▼
 0 => "5361"
 1 => "5361"
]
"category_id" => array:2 [▼
 0 => "1"
 1 => "3"
]
"field_1" => array:2 [▶]
"field_2" => array:2 [▶]
"field_3" => array:2 [▶]
"field_4" => array:2 [▶]
"field_5" => array:1 [▶]
"price" => array:2 [▶]
"update_plan" => "Save Plans"
]```


CodePudding user response:

You can insert like this

Eloquent approach

Model::insert($data); 

Query Builder approach

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

CodePudding user response:

Try like this

foreach($data['description'] as $key=>$desc){
    $save_data=[
        'description'=>$desc,
        'site_id'=>$data['site_id'][$key],
        'category_id'=>$data['category_id'][$key],
        'feature_field_1'=>$data['field_1'][$key],
        'feature_field_2'=>$data['field_2'][$key],
        'feature_field_3'=>$data['field_3'][$key],
        'feature_field_4'=>$data['field_4'][$key],
        'feature_field_5'=>$data['field_5'][$key],
        'price'=>$data['price'][$key]
    ]
    \DB::table('table')->insert($save_data);
}
  • Related