14

I have an observer for my User model. Inside my observer->created event i have some code.

public function created(User $user)
{
    sendEmail();
}

So, the idea is, when a user is created, the system will send for the user email notification that the account was created.

Question: When the database is seeding, it also calls this method 'created' and sends users (that are in the seeds) email notification. So, my question is, how can i check, probably inside this 'created' method if at the moment laravel is seeding data -> do not send email of do not run the 'created' observer method.

Tried to google, found something, but not working correct. Something like YourModel::flushEventListeners();

julianstark999
  • 3,450
  • 1
  • 27
  • 41

4 Answers4

25

You can use YourModel::unsetEventDispatcher(); to remove the event listeners for a model temporary.

If you need them after seeding in the same execution, you can read the dispatchers, unset them and then set them again.

$dispatcher = YourModel::getEventDispatcher();
// Remove Dispatcher 
YourModel::unsetEventDispatcher();

// do stuff here

// Re-add Dispatcher
YourModel::setEventDispatcher($dispatcher);
Eugene van der Merwe
  • 4,390
  • 1
  • 35
  • 48
julianstark999
  • 3,450
  • 1
  • 27
  • 41
  • Dear Julian, thank you for your answer. Could you please say, where should i write this code, in which file. And another, is there any way to disable only for my current event? (created). As i understand, your code will disable all possible events. What if some events should work in future during seeding. Thank you – Станислав Строянецкий Feb 18 '21 at 08:51
  • You can call this inside the spcific seeder. If you need the events before or after seeding the users, you can use the extended example to only disable the events for this part of the code – julianstark999 Feb 18 '21 at 13:47
  • This will affect all models not just `YourModel` so you can apply that on the base model – medilies Jul 24 '22 at 16:35
10
 namespace Database\Seeders; 
                                                    
    use App\Models\Blog; 
    use Illuminate\Database\Seeder;

    class BlogsTableSeeder extends Seeder
    {
         public function run()
         {
   
           Blog::withoutEvents(function ()  {
                
            // normally
                 Blog::factory()
                 ->times(10)      
                 ->hasUploads(1)        //hasOne
                 ->hasComments(2)       //hasMany                           
                 ->create();

            });


         }
    }
Tjeu Moonen
  • 187
  • 2
  • 6
9

You may mute event with WithoutModelEvents trait

use Illuminate\Database\Console\Seeds\WithoutModelEvents;

class SomeSeeder extends Seeder
{
    use WithoutModelEvents;
    
    public function run()
    {
        User::factory( 30 )->create();
    }
}

or you may try createQuietly method of a factory, for example

class SomeSeeder extends Seeder
{
    public function run()
    {
        User::factory( 30 )->createQuietly();
    }
}
Ihar Aliakseyenka
  • 9,587
  • 4
  • 28
  • 38
3

You could use the saveQuietly() function https://laravel.com/docs/8.x/eloquent#saving-a-single-model-without-events This allows you to disable all events for a single model.

If you wanna disable a single event for a single model, read about it here: http://derekmd.com/2019/02/conditionally-suppressing-laravel-event-listeners/

Musti
  • 470
  • 2
  • 11