Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 887
Jack Avatar asked May 27 '15 20:05

Jack


People also ask

How does Laravel seeding work?

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.

Which command to run seeding in Laravel?

Laravel Seeding Creating a Seeder To create seeders, you may use the make:seeder Artisan command. All seeders generated will be placed in the database/seeds directory. Generated seeders will contain one method: run . You may insert data into your database in this method.

How do I roll back seeding in Laravel?

use Undo Seeder for Laravel. When you install UndoSeeder, the following artisan commands are made available: db:seed-undo Undo seeds in the seeds directory. db:seed-refresh Undo seeds run seeds again.

What are seeds in Laravel?

Laravel offers a tool to include dummy data to the database automatically. This process is called seeding. Developers can add simply testing data to their database table using the database seeder. It is extremely useful as testing with various data types allows developers to detect bugs and optimize performance.


1 Answers

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(); 
like image 123
user1669496 Avatar answered Sep 26 '22 00:09

user1669496