使用雄辩的ORM批量插入Laravel

我们如何使用Eloquent ORM在Laravel中执行批量数据库插入?

我想在Laravel中完成这个工作: https ://stackoverflow.com/a/10615821/600516但是我得到以下错误。

SQLSTATE [HY093]:无效的参数编号:混合的名称和位置参数。

你可以使用Eloquent::insert()

例如:

 $data = array( array('name'=>'Coder 1', 'rep'=>'4096'), array('name'=>'Coder 2', 'rep'=>'2048'), //... ); Coder::insert($data); 

我们可以更新GTF答案来更新时间戳

 $data = array( array( 'name'=>'Coder 1', 'rep'=>'4096', 'created_at'=>date('Ymd H:i:s'), 'modified_at'=> date('Ymd H:i:s') ), array( 'name'=>'Coder 2', 'rep'=>'2048', 'created_at'=>date('Ymd H:i:s'), 'modified_at'=> date('Ymd H:i:s') ), //... ); Coder::insert($data); 

更新:为了简化date,我们可以用@Pedro Moreira的build议

 $now = Carbon::now('utc')->toDateTimeString(); $data = array( array( 'name'=>'Coder 1', 'rep'=>'4096', 'created_at'=> $now, 'modified_at'=> $now ), array( 'name'=>'Coder 2', 'rep'=>'2048', 'created_at'=> $now, 'modified_at'=> $now ), //... ); Coder::insert($data); 

UPDATE2:对于laravel 5,请使用updated_at而不是modified_at

 $now = Carbon::now('utc')->toDateTimeString(); $data = array( array( 'name'=>'Coder 1', 'rep'=>'4096', 'created_at'=> $now, 'updated_at'=> $now ), array( 'name'=>'Coder 2', 'rep'=>'2048', 'created_at'=> $now, 'updated_at'=> $now ), //... ); Coder::insert($data); 

对于读这个的人来说,看看createMany()方法 。

 /** * Create a Collection of new instances of the related model. * * @param array $records * @return \Illuminate\Database\Eloquent\Collection */ public function createMany(array $records) { $instances = $this->related->newCollection(); foreach ($records as $record) { $instances->push($this->create($record)); } return $instances; } 

这就是你如何以更雄辩的方式做到这一点,

  $allintests = []; foreach($intersts as $item){ //$intersts array contains input data $intestcat = new User_Category(); $intestcat->memberid = $item->memberid; $intestcat->catid= $item->catid; $allintests[] = $intestcat->attributesToArray(); } User_Category::insert($allintests); 

或者在使用Laravel的工厂(创build10个用户)时:

 $users = factory(App\User::class, 10)->make()->toArray(); App\User::insert($users); 
 $start_date = date('Ymd h:m:s'); $end_date = date('Ymd h:m:s', strtotime($start_date . "+".$userSubscription['duration']." months") ); $user_subscription_array = array( array( 'user_id' => $request->input('user_id'), 'user_subscription_plan_id' => $request->input('subscription_plan_id'), 'name' => $userSubscription['name'], 'description' => $userSubscription['description'], 'duration' => $userSubscription['duration'], 'start_datetime' => $start_date, 'end_datetime' => $end_date, 'amount' => $userSubscription['amount'], 'invoice_id' => '', 'transection_datetime' => '', 'created_by' => '1', 'status_id' => '1', ), array( 'user_id' => $request->input('user_id'), 'user_subscription_plan_id' => $request->input('subscription_plan_id'), 'name' => $userSubscription['name'], 'description' => $userSubscription['description'], 'duration' => $userSubscription['duration'], 'start_datetime' => $start_date, 'end_datetime' => $end_date, 'amount' => $userSubscription['amount'], 'invoice_id' => '', 'transection_datetime' => '', 'created_by' => '1', 'status_id' => '1', ) ); dd(UserSubscription::insert($user_subscription_array)); 

UserSubscription是我的模型名称。 如果插入成功,则返回“true”,否则返回“false”。

也许更Laravel的方式来解决这个问题是使用一个集合,并利用时间戳利用模型插入循环。

 <?php use App\Continent; use Illuminate\Database\Seeder; class InitialSeeder extends Seeder { /** * Run the database seeds. * * @return void */ public function run() { collect([ ['name' => 'América'], ['name' => 'África'], ['name' => 'Europa'], ['name' => 'Asia'], ['name' => 'Oceanía'], ])->each(function ($item, $key) { Continent::forceCreate($item); }); } } 

编辑:

对不起,我的误解。 对于批量插入这可能会有所帮助,也许有了这个,你可以做出好的播种和优化他们一点。

 <?php use App\Continent; use Carbon\Carbon; use Illuminate\Database\Seeder; class InitialSeeder extends Seeder { /** * Run the database seeds. * * @return void */ public function run() { $timestamp = Carbon::now(); $password = bcrypt('secret'); $continents = [ [ 'name' => 'América' 'password' => $password, 'created_at' => $timestamp, 'updated_at' => $timestamp, ], [ 'name' => 'África' 'password' => $password, 'created_at' => $timestamp, 'updated_at' => $timestamp, ], [ 'name' => 'Europa' 'password' => $password, 'created_at' => $timestamp, 'updated_at' => $timestamp, ], [ 'name' => 'Asia' 'password' => $password, 'created_at' => $timestamp, 'updated_at' => $timestamp, ], [ 'name' => 'Oceanía' 'password' => $password, 'created_at' => $timestamp, 'updated_at' => $timestamp, ], ]; Continent::insert($continents); } } 

我更喜欢这个更干净的代码。 Laravel 5.2

 $feed_types = [ [ 'name' => 'Photo', 'slug' => 'photo'], [ 'name' => 'Album', 'slug' => 'album'] ]; foreach ($feed_types as $feed_type) { FeedType::create($feed_type); }