Last active
May 15, 2020 07:13
-
-
Save lattespirit/ae64efa2d4e88deaa1ce9929912da0a8 to your computer and use it in GitHub Desktop.
Quickly create millions records with Laravel Seeder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use App\User; | |
use Faker\Factory; | |
use Illuminate\Support\Str; | |
use Illuminate\Database\Seeder; | |
use Illuminate\Support\LazyCollection; | |
class DatabaseSeeder extends Seeder | |
{ | |
/** | |
* Seed the application's database. | |
* | |
* @return void | |
*/ | |
public function run() | |
{ | |
$total = 10000000; | |
$eachTime = 100; | |
$lazy = LazyCollection::make(function () use ($total) { | |
$faker = Factory::create(); | |
for ($i=0; $i < $total; $i++) { | |
$user = [ | |
'name' => $faker->name, | |
'email' => $faker->safeEmail, | |
'email_verified_at' => now(), | |
'password' => '$2y$10$TKh8H1.PfQx37YgCzwiKb.KjNyWgaHb9cbcoQgdIVFlYg7B77UdFm', // secret | |
'remember_token' => Str::random(10), | |
]; | |
yield $user; | |
} | |
}); | |
foreach ($lazy->chunk($eachTime) as $users) { | |
User::insert($users->toArray()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment