0

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
        )

)

bay_beast
  • 11
  • 5

1 Answers1

0

so from your comments i assume you want to zip values from arrays accordingly like

array(
  [
    $branches[0],
    $dates[0]
    ...
  ],
  [
    $branches[1],
    $dates[1]
    ...
  ],
)

then

// preparing data for models
    $combined = array_map(
      null,
      $request->get('branches'),
      $request->get('date'),
      $request->get('auditors'),
      $request->get('disbursed'),
      $request->get('sample_selected'),
      $request->get('days_cases'),
      $request->get('days_branches'),
      $request->get('total_days'),
    );
    
    // saving
    foreach ($combined as $data) {
      // rename fields accordingly
      // don't forget to provide $fillable array in ModelName
      ModelName::create([        
        'branches' => $data[0],
        'date' => $data[1],
        'auditors' => $data[2],
        'disbursed' => $data[3],
        'sample_selected' => $data[4],
        'days_cases' => $data[5],
        'days_branches' => $data[6],
        'total_days' => $data[7]
      ]);
    }

a bit more nicely looking solution if you provide custom fill method for model

//Model
    public function customFill($data){
      // calling laravel default fill method
      $this->fill([        
        'branches' => $data[0],
        'date' => $data[1],
        'auditors' => $data[2],
        'disbursed' => $data[3],
        'sample_selected' => $data[4],
        'days_cases' => $data[5],
        'days_branches' => $data[6],
        'total_days' => $data[7]
      ]);
      // returning filled model to be able to use fluent methods
      return $this;
    }

then in controller

    // preparing data for models
    ...
    // saving
    foreach ($combined as $data) {
      (new ModelName)->customFill($data)->save();
    }
Ol D. Castor
  • 543
  • 4
  • 12