Laravel Bulk Insert Using Eloquent ORM
You can pass in an array to the "insert" method and within the array which contains another array of data.
<?php use App\Models\Post; Post::insert([ ['name'=>'Post 1', 'slug' => 'post-1', 'body' => 'Post 1 body', 'published_at' => now()], ['name'=>'Post 2', 'slug' => 'post-2', 'body' => 'Post 2 body', 'published_at' => now()], ]);
<?php use App\Models\Post; $posts = [ ['name'=>'Post 1', 'slug' => 'post-1', 'body' => 'Post 1 body', 'published_at' => now()], ['name'=>'Post 2', 'slug' => 'post-2', 'body' => 'Post 2 body', 'published_at' => now()], ]; Post::insert($posts);
Leave a reply