Usually during testing, you may want to create a record using a model without dispatching events.
There's a really easy way to do this using createQuietly
.
Here's an example of how it works on a standard model:
$user = User::createQuietly(['name' => 'Mabel', 'email' => 'mabel@codecourse.com']);
Using createQuietly
won't trigger any events, meaning anything you've defined in a model observer won't get triggered.
Since this is more useful for testing, here's an example of coupling this with a factory.
User::factory(3)->createQuietly();
The above example creates 3 users without dispatching events.
There are other variations on the Quietly
method extension, too, like createOneQuietly
and createManyQuietly
.