我想知道如何访问具有 id 或名称代码的输入元素,例如row_1_firstname 和 row_2_firstname - 当您不确切知道有多少行时,如何进行批量插入以及如何查询具有不确定行数的 Input::get 或 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
}
}
最佳答案
我会将输入命名为数组 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);
}
关于php - Laravel 中的批处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26175221/