Laravel - Don't fire events when seeding database

Viewed 1072

I have a service provider which defines which models to observe in the boot method, e.g:

Task::observe(TaskObserver::class);

In TaskObserver.php I am listening for the updated event that fires when the model is updated.

When this happens, the Notification facade send an email to a collection of clients:

Notification::send($clients, new TaskUpdated($task));

Everything is working fine, but the problem I have is when I seed my database, I get an error [Symfony\Component\Debug\Exception\FatalThrowableError] __clone method called on non-object

My table seeder makes use of a factory which simply returns the values needed to create a model.

Why is the updated event firing when a Task is being created by the factory? And is there a way of avoiding the error when running the seeder?

Seeder:

class DatabaseSeeder extends Seeder
{
    public function run()
    {
        ...
        $this->call(TasksTableSeeder::class);
        ...
    }
}

class TasksTableSeeder extends Seeder
{
    public function run()
    {
        factory(Task::class, 50)->create();
    }
}

$factory->define(Task::class, function (Faker $faker) {
    return [
        'title' => $faker->sentence,
        'description' => $faker->sentence(8),
        'created_at' => Carbon::now(),
        'updated_at' => Carbon::now(),
    ];
});
1 Answers

You can use createQuietly instead of create in your seeder.

public function run()
{
     Contact::factory(50)->createQuietly();
}
Related