php - how to insert bulk data at a time in laravel? -
hi trying insert multiple rows in database @ time. can save single row @ time. have done following:
public function addnewpricerevision($data){ $pricerevision=new productpricerevision( [ 'product_id'=>$data->product_id, 'invent_price'=>$data->invent_price, 'revised_price'=>$data->revised_price, 'effective_date'=>$data->effective_date, 'deleted'=>$data->deleted, 'remark'=>$data->remark, 'user'=>$data->user, 'date'=>$data->updated_at, ] ); $pricerevision->save(); return common::getjsonresponse(true, 'new price revision created successfully!', 200); }
try convert objects arrays , create array multiple arrays:
$data = array($data1->toarray(), $data2->toarray());
if need add timestamps, must manually:
$date = date('y-m-d h:i:s'); $data1 = $data1->toarray(); $data1['created_at'] = $date; $data1['updated_at'] = $date; $data2 = $data2->toarray(); $data2['created_at'] = $date; $data2['updated_at'] = $date; $data = array($data1, $data2);
then use mass assignment feature create rows:
productpricerevision::create($data);
and don't forget fill $fillable
array inside productpricerevision
model:
protected $fillable = [ 'product_id', 'invent_price', 'revised_price', 'effective_date', 'deleted', 'remark', 'user', 'updated_at' ];
Comments
Post a Comment