Home > database >  How to save multiple rows of table data into the laravel database
How to save multiple rows of table data into the laravel database

Time:06-08

Essential Question: How to upload my table data so it can be correctly saved?

In my view, I have a table with 3 columns and multiple rows. The rows are populated correctly with a foreach: When the data comes into the controller using this: $input = $request->all(); I am not getting a well-organized series of arrays and I am totally failing at getting the data saved into the DB. My data:

Array
(
    [branches] => Array
        (
            [0] => 630
            [1] => 632
            [2] => 715
        )

    [date] => Array
        (
            [0] => 06/14/2022
            [1] => 06/23/2022
            [2] => 06/29/2022
        )

    [auditiors] => Array
        (
            [0] => 3
            [1] => 5
            [2] => 4
        )

    [disbursed] => Array
        (
            [0] => 797
            [1] => 32
            [2] => 234
        )

    [sample_selected] => Array
        (
            [0] => 798
            [1] => 2324
            [2] => 1233
        )

    [days_cases] => Array
        (
            [0] => 765
            [1] => 21334
            [2] => 12
        )

    [days_branches] => Array
        (
            [0] => 32
            [1] => 123
            [2] => 1223
        )

    [total_days] => Array
        (
            [0] => 797
            [1] => 21457
            [2] => 1235
        )

)

CodePudding user response:

$branches = $request->branches;
 $data = $request->date;
 // and so on

 foreach ($branches as $key => $value) {
 YOURMODEL::create([        
    'branches' => $value[$key]
    ]);
  }

 foreach ($data as $key => $value) {
 YOURMODEL::create([        
    'data' => $value[$key]
    ]);
  }
// and so on

CodePudding user response:

foreach ($request->total_days as $day) {
        $input = ModelName::create(array(
            'columnname1' => $day['inputName'],
            'columnname1' => $day['inputName'],
            'columnname1' => $day['inputName']               

        ));
      }

//and so on another foreach loop with $request->arrayName

  • Related