将多个数据保存到Laravel中的数据库中

I have a form within a table that has different fields. I want to be able to store all the data once. I hve search Stack overflows, all the answer i got did not solve the issue.Hence, I have to post my own question. The View

<td><input type="text" name="sflt[]" value="{{ $r['fltno'] }}" readonly="readonly" class="form-control"/></td>
<td><input type="text" name="smodel[]" value="{{ $r['model']}}" readonly="readonly" class="form-control"/></td> 
<td><input type="text" name="sengine[]" value="{{ $r['engine_type'] }}" readonly="readonly" class="form-control"/></td> 
<td><input type="text" name="sloc[]" value="{{ $r['location'] }}" readonly="readonly" class="form-control"/></td> 
<td><input type="text" name="sstye[]" value="{{ $sty }}" readonly="readonly" class="form-control"/></td> 
<td><input type="text" name="snsvr[]" value="{{ $nsvr}}" readonly="readonly" class="form-control"/></td> 

Controller

          $data = [];
          **//Get the input variables**
          $fltno= $request['sflt'];
          $model = $request['smodel'];
          $engine = $request['sengine'];
          $loc  = $request['sloc'];

// Store each varaibles as you fetch into the empty array

      foreach($fltno as $fltno)
         {
      $data[] = [
                'fltno'=>$request['sflt'],
                'model'=>$request['smodel'],
                'engine'=>$request['sengine'],
                loc'=>$request['sloc'],
               'serviceType'=>$request['sstye'],
               'nextSvr'=> $request['snsvr'] 
                ];
        }

       ModelName::insert($data); 

When I execute this, it throws error of: Invalid argument supplied for foreach()

Please, what am i doing wrong or what is the best way to insert all these data at once into the Database

Hi Dave I think you want to do something like this

$data = $request->all();
$finalArray = array();
foreach($data as $key=>$value){
   array_push($finalArray, array(
                'fltno'=>$value['sflt'],
                'model'=>$value['smodel'],
                'engine'=>$value['sengine'],
                'loc'=>$value['sloc'],
                'serviceType'=>$value['sstye'],
                'nextSvr'=> $value['snsvr'] )
   );
});

Model::insert($finalArray);

This will submit all data. I am assuming you are getting array in request with these keys.

Hope this will help.

this is my working example you can implement in your code.

$competition_que = [];
        foreach ($input['options'] as $key => $value) {
            $competition_que[$key]['competition_id'] = $request->competition_id;
            $competition_que[$key]['question_id'] = $question->id;
            $competition_que[$key]['options'] = $input['options'][$key];
            $competition_que[$key]['created_at'] = Carbon::now();
            $competition_que[$key]['updated_at'] = Carbon::now();
            $competition_que[$key]['is_answer'] = ($key == $input['is_answer']) ? 'true' : 'false';
        }

//        return $competition_que;
        CompetitionAnswer::insert($competition_que);

I think you are making mistake when retrieve the request body

$fltno = $request->input('sflt');

for the foreach I think you can do this

$fltnos = $request->input('sflt');

foreach($fltnos as $fltno) {
}

If you are using ORM

$fltnos = $request->input('sflt');
$models = $request->input('smodel');
$engines = $request->input('sengine');
$locs  = $request->input('sloc');

foreach($fltnos as $key => $fltno) {
    $modelName = new ModelName;
    $modelName->fltno = $fltno;
    $modelName->model = isset($models[$key]) ? $models[$key] : ''; // this is just workaround, you must make sure all field have same length if want to do this
    ...
    $modelName->save()
}

I think you can do it this way

$fltnos = $request->input('sflt', []); // second parameter is to set default value 
$models = $request->input('smodel', []);
$engines = $request->input('sengine', []);
$locs  = $request->input('sloc', []);

also you need to validate your bulk data prior to save

you can do it like this

public function store(Request $request)
{
    $validatedData = $request->validate([
        'sflt.*' => 'email|unique:users',
        'smodel.*' => 'required'
    ]);


if(validatedData) {

foreach($fltnos as $key => $fltno) {
    $modelName = new ModelName;
    $modelName->fltno = $fltno;
    ...
    $modelName->save()
}

}

}