[9.x] Adds WithoutModelEvents
trait for running seeds without Model Events
#39922
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This pull request proposes a creation of a public trait —
WithoutModelEvents
— that prevents Model Events from being executed while running seeds.Laravel developers often use Model events for firing queueable or not queueable tasks, that will send emails, index their Laravel Scout data, logging, etc. And, while useful, I don't necessarily want these tasks to be executed by my seeders.
So, having this
WithoutModelEvents
trait, which is very similar to the testing traitWithoutModelEvents
, it's super useful, especially when using Factories on my seeds:Regarding the implementation there are two things to keep in mind:
$this->call
to call multiple seeders, theWithoutModelEvents
propagates down. Let's see two examples:WithoutModelEvents
trait, no Models Events are ever fired when using this seeder:WithoutModelEvents
trait, so Model Events get fired by theUserSeeder::class
but not byUserWithoutModelEventsSeeder::class
: