将多个数据一次性保存到laravel中的数据库中

3htmauhk  于 2021-06-20  发布在  Mysql
关注(0)|答案(4)|浏览(452)

我在一个表中有一个表单,它有不同的字段。我希望能够一次存储所有数据。我的搜索堆栈溢出,我得到的所有答案都没有解决问题。因此,我必须张贴我自己的问题。景色

<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>

控制器

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

//将每个变量存储到空数组中

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);

当我执行此命令时,它抛出错误:为foreach()提供的参数无效
请问,我做错了什么,或者把这些数据一次插入数据库的最佳方法是什么

pdtvr36n

pdtvr36n1#

嗨,戴夫,我想你想做这样的事

$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);

这将提交所有数据。我假设您正在请求使用这些密钥的数组。
希望这会有帮助。

k7fdbhmy

k7fdbhmy2#

这是我的工作示例,您可以在代码中实现。

$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);
e7arh2l6

e7arh2l63#

我想你可以这样做

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

您还需要在保存前验证批量数据
你可以这样做

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()
}

}

}
nmpmafwu

nmpmafwu4#

我认为你在检索请求主体时犯了错误

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

对于foreach,我想你能做到

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

foreach($fltnos as $fltno) {
}

如果您正在使用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()
}

相关问题