So I was working with laravel seeds and came across this amazing article on how to speed them up.
http://bensmith.io/speeding-up-laravel-seeders
I have tested this method and yes the seed time has dropped to half for my most my seeds.
The only problem that I face is also described is that the make()
method on the factory does not generate timestamps and some of my tests start complaining now.
Instead of modifying all of my seeds to make them seed timestamps manually, is there a way I can force the make()
method on the factories to do it for me?
Example seed
<?php
use Illuminate\Database\Seeder;
use Uppdragshuset\AO\Tenant\Models\CommentType;
use Uppdragshuset\AO\Tenant\Models\User;
use Uppdragshuset\AO\Tenant\Models\Document;
use Uppdragshuset\AO\Tenant\Models\Comment;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Output\ConsoleOutput;
class CommentTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$output = new ConsoleOutput;
$bar = new ProgressBar($output);
$this->command->info("Seeding Orders");
$bar->setFormat('verbose');
$bar->start();
$comment_types = CommentType::all(['id']);
$users = User::all(['id']);
$documents = Document::all(['id']);
$comments = [];
for($i = 0; $i < 50; $i++){
$bar->advance();
$comments[] = factory(Comment::class)->make([
'comment_type_id' => $comment_types->shuffle()->first()->id,
'user_id' => $users->shuffle()->first()->id,
'commentable_id' => $documents->shuffle()->first()->id,
])->toArray();
}
Comment::insert($comments);
$bar->finish();
$this->command->info("\n\r");
}
}
So I have the option of putting
'created_at' => Carbon\Carbon::now(),
'updated_at' => Carbon\Carbon::now(),
in the seed and all others but is there a better way to override the make
method?