laravel中的批处理

Y玛瑙

我想知道如何访问具有id或名称代码(例如row_1_firstname和row_2_firstname)的Input元素-当您不确切知道行的数量,如何进行批量插入以及如何查询Input时::获取或输入:: all(),行和行的数目不确定,用于插入和更新过程

public function store()
{
     $array = Input::all();
     foreach($array as $element) {
          // do stuff in here
          // store data in a array for batch insert and update processing 
     }
}
马尔辛·纳比亚列克(MarcinNabiałek)

我将输入命名为数组,firstname[]并将隐藏的输入命名为ids[]id以进行编辑

public function store()
{       
     $names = Input::get('firstname');
     $ids = Input::get('ids');

     for ($i=0, $c = count($ids); $i<$c; ++$i) {
         echo $names[$i].' was set for id '.$ids[$i];
     }
}

编辑

如果要将多个记录插入到数组中,可以通过以下方式进行操作:

public function store()
{       
     $names = Input::get('firstname');
     $surnames = Input::get('surname');

     $data_array = [];    
     for ($i=0, $c = count($names); $i<$c; ++$i) {
          $record = [];
          $record['name'] = $names[$i];
          $record['surname'] = $surnames[$i];
          $data_array[] = $record;  
     }
     DB::table('users')->insert($data_array);
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章