3

因此,我在使用 laravel 种子时遇到了这篇关于如何加速种子的惊人文章。

http://bensmith.io/speeding-up-laravel-seeders

我已经测试过这种方法,是的,我的大多数种子的种子时间已经下降到一半。

还描述了我面临的唯一问题是make()工厂上的方法没有生成时间戳,我的一些测试现在开始抱怨。

与其修改我所有的种子以手动使它们成为种子时间戳,有没有办法可以强制make()工厂的方法为我做这件事?

示例种子

<?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");
    }
}

所以我可以选择放

'created_at' => Carbon\Carbon::now(),
'updated_at' => Carbon\Carbon::now(),

在种子和所有其他人中,但有没有更好的方法来覆盖该make方法?

4

0 回答 0