Home  >  Article  >  Backend Development  >  Laravel batch insert data

Laravel batch insert data

angryTom
angryTomforward
2019-10-16 11:14:4210016browse

Laravel batch insert data

Implementation method

$data= [
  ['name'=>'111'],
  ['name'=>'222'],
];
DB::table('xxx')->insert($data);

can also be added to the Model

class BaseModel extend Model {  
 
    public function addAll(Array $data)
    {
        return DB::table($this->getTable())->insert($data);
    }
 
}
class User extend BaseModel{
 
}
//使用时$user = new User();
$data= [
  ['name'=>'222'],
  ['name'=>'222'],
];
 
$user->addAll($data);

For more PHP related knowledge, please visit PHP Chinese website!

The above is the detailed content of Laravel batch insert data. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:左鹏个人博客. If there is any infringement, please contact admin@php.cn delete