Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel. Disable observer methods if the database is seeding

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();

like image 510

People also ask

How do I seed a database in Laravel?

Laravel includes the ability to seed your database with data using seed classes. All seed classes are stored in the database/seeders directory. By default, a DatabaseSeeder class is defined for you. From this class, you may use the call method to run other seed classes, allowing you to control the seeding order.

What are observers in Laravel?

Definition Observers in Laravel help you to fire multiple events or do a CRUD operation in database! When you create an observer, it will listen to a specific condition for a model, e.g: if user create a post and he/she achieved a certain level, the observer will fire related method (event) and do what ever you wrote!

How to add postobserver class in Laravel appserviceprovider?

Now you have to register this class in Laravel’s Service Container, so we will add this class in AppServiceProvider ‘s boot () method by telling Post model to observe the PostObserver class, like below: In our PostObserver class, Laravel created some function by default like created, updated and others.

How do I seed a databaseseeder class?

By default, a DatabaseSeeder class is defined for you. From this class, you may use the call method to run other seed classes, allowing you to control the seeding order. Mass assignment protection is automatically disabled during database seeding. To generate a seeder, execute the make:seeder Artisan command.


4 Answers

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);
like image 156
julianstark999 Avatar answered Oct 12 '22 23:10

julianstark999


 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();

            });


         }
    }
like image 43
Tjeu Moonen Avatar answered Oct 13 '22 00:10

Tjeu Moonen


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();
    }
}
like image 20
Ihar Aliakseyenka Avatar answered Oct 13 '22 00:10

Ihar Aliakseyenka


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/

like image 44
Musti Avatar answered Oct 12 '22 22:10

Musti