Is it possible to prevent Laravel running Model Events when the database is being Seeded?

Jack picture Jack · May 27, 2015 · Viewed 8.9k times · Source

Laravel's seeder runs a variety of Model Events on my models which trigger New Order notification emails, among other things, from the Product::saved() Model Event.

This significantly slows down database seeding. Is it possible to detect whether a Seed is being ran and if so, tell Laravel not to run the Model Events?

Answer

user1669496 picture user1669496 · May 27, 2015

There are functions on the Model class which will allow you to ignore events.

Before using a model to seed, you will need to do something like this...

YourModel::flushEventListeners();