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.
What is the reason for this PR?
Author's checklist
Summary of changes
When using FakerPHP in database factories, such as in Laravel, I frequently encounter the need for unique values for a specific model or column. For example, unique emails for that model or a non-auto-incrementing ID that needs to be set.
Consider this implementation of a Laravel database factory:
Because of the
->unique()
method, we obtain a uniqueexternal_id
for each user. However, we may have another model that also requires unique numbers. Ideally, the IDs should be reset for this model. However, calling->unique(reset: true)
would reset the entire generator, causing it to reset on every call.Therefore, I propose an option to add a key so that people can have multiple unique generators. This would allow us to have an independent set of unique numbers for both factories and models.
What do you think about this?
I'm not sure whether you consider
$uniqueGenerator
internal or if removing$uniqueGenerator
and adding$uniqueGenerators
is considered a breaking change.Maybe this should be implemented as a new method, but I couldn't think of a better name than
keyedUnique()
. It appears that there is some confusion surrounding the usage of->unique()
in general.laravel/framework#46287
laravel/laravel#5137
Review checklist
CHANGELOG.md