使用插入方法,如:
$model->insert([ ['email' => 'taylor@example.com', 'votes' => 0], ['email' => 'dayle@example.com', 'votes' => 0] ]);
另见:http://laravel.com/docs/5.1/queries#inserts
编辑:
更新到您的代码:
public function postSpecs(Request $request) { $specs = $request->except(['_token', 'deviceid']); $data = array(); foreach($specs as $key=>$val) { if($val == '') continue; if(Spec::where('category', $key)->where('device', $request->deviceid)->exists()) { $spec = Spec::where('category', $key)->where('device', $request->deviceid)->first(); $spec->value = $val; $spec->save(); } else { $data[]['category'] = $key; $data[]['device'] = $request->deviceid; $data[]['value'] = $val; } } Spec::insert($data); }
虽然这并不完美,但它可以为您节省大量的查询.否则你必须使用原始查询(未经测试!):
INSERT INTO spec (id,category,device,value) VALUES (1,2,3),(4,5,6) ON DUPLICATE KEY UPDATE id=LAST_INSERTED_ID(id)