Replies: 1 comment 1 reply
-
For anyone looking for a temporary solution, I just whipped up this: Factory::macro('firstOrCreate', function (array $attributes = [], array $values = [], ?Model $parent = null) {
if ($existing = $this->model::where($attributes)->first()) {
return $existing;
}
return $this->create([...$attributes, ...$values], $parent);
}); |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
It would be great if Eloquent model factories could have a
firstOrCreate
method. A use case for this would be in seeders when you might want to use model factories but want to be able to re-run seeders without duplicating data.Beta Was this translation helpful? Give feedback.
All reactions