我在POST中的’Cylinders’数据数组中得到一个数组:
Array ( [serie] => Array ( [0] => 1234 [1] => 3545 ) [seriesap] => Array ( [0] => 1234234 [1] => 345345 ) [type] => Array ( [0] => 4546 [1] => csdfwe ) [admission] => Array ( [0] => 04-05-2015 [1] => 04-05-2015 ) [invoice] => Array ( [0] => fei76867 [1] => feiasodjf ) )
现在,键内的字段:系列,类型,入场等不会改变,但这些键内的信息确实发生变化,我的意思是那里甚至可能有15个项目.
最后我需要保存到数据库:
$cylinder = new Cylinder(); $cylinder->serie = ??; $cylinder->seriesap = ??; $cylinder->type = ??; $cylinder->admission = ??; $cylinder->invoice = ??; $cylinder->save
我怎样才能完成这项任务并保存所有气缸?
我已经尝试过所有可以想到的东西似乎没什么用.
/编辑/
这就是我到目前为止所做的事情:
$cyldata = $_POST['cylinder']; //this is the post from top. $num_elements = 0; while($num_elements < count($cyldata['serie'])){ $cylinder = new Cylinder(); $cylinder->serie = $cyldata['serie'][$num_elements]; $cylinder->type = $cyldata['type'][$num_elements]; $cylinder->admission = $cyldata['admission'][$num_elements]; $cylinder->seriesap = $cyldata['seriesap'][$num_elements]; $cylinder->save $num_elements++; }
但感觉很难看,所有这些豁免都不对.如果你问我,那会很脏.
首先,您需要将输入数据转换为另一种格式:
原文链接:https://www.f2er.com/laravel/240128.html$cyldata = $_POST['cylinder']; //this is the post from top. $num_elements = 0; $sqlData = array(); while($num_elements < count($cyldata['serie'])){ $sqlData[] = array( 'serie' => $cyldata['serie'][$num_elements],'type' => $cyldata['type'][$num_elements],'admission' => $cyldata['admission'][$num_elements],'seriesap' => $cyldata['seriesap'][$num_elements],'invoice' => $cyldata['invoice'][$num_elements],// you miss this field,aren't you? 'created_at' => Carbon\Carbon::now(),// only if your table has this column 'updated_at' => Carbon\Carbon::now(),// only if your table has this column ); $num_elements++; }
其次,使用Fluent查询构建器执行批量插入:
DB::table('table_name')->insert($sqlData);
注意:如果您的表具有这些字段,则created_at和updated_at将显示在此处.使用Eloquent模型时,这些字段会自动更新.但是,我们不使用Eloquent,因此我们必须手动将值分配给这些字段.