Skip to content

Instantly share code, notes, and snippets.

@lattespirit
Last active May 15, 2020 07:13
Show Gist options
  • Save lattespirit/ae64efa2d4e88deaa1ce9929912da0a8 to your computer and use it in GitHub Desktop.
Save lattespirit/ae64efa2d4e88deaa1ce9929912da0a8 to your computer and use it in GitHub Desktop.
Quickly create millions records with Laravel Seeder
<?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