How to save multiple inputs of rows in the same column of database?

前端 未结 1 738
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-14 13:16

Database table

id  | title | reading | writing | speaking

form.blade.php

Language<
相关标签:
1条回答
  • 2021-01-14 13:42

    Use multiple inputs with same name like this

    <input name="TitleText[]" />
    <select name="ReadingText[]" >
    ...
    <select name="langWrittingText[]">
    ...
    <select name="SpeakingText[]">
    ...
    

    This way the data will be posted in the form of an array. Assuming that all the inputs/select are required to be filled by the user

    In your controller you can do something like this

    $count = count($input['TitleText']); // here we will know how many entries have been posted
    $languages = array();
    for($i=0; $i<$count; $i++){
       if(!empty($input['TitleText'][$i])){
         array_push($languages, array( // iterate through each entry and create an array of inputs
          'title' => $input['TitleText'][$i], 
          'reading' => $input['ReadingText'][$i], 
          'writting' => $input['WrittingText'][$i],
          'speaking' => $input['SpeakingText'][$i]
         ));
       }
    }
    Languages::insert($languages); // save the array of models at once
    

    Hope this helps.

    0 讨论(0)
提交回复
热议问题