Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.x] Allow overwriting the column for unique_user_value validation #8852

Merged
merged 2 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/Validation/UniqueUserValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ class UniqueUserValue
{
public function validate($attribute, $value, $parameters, $validator)
{
[$except] = array_pad($parameters, 1, null);
[$except, $column] = array_pad($parameters, 2, null);

$column ??= $attribute;

$existing = User::query()
->where($attribute, $value)
->where($column, $value)
->first();

if (! $existing) {
Expand Down
11 changes: 11 additions & 0 deletions tests/Validation/UniqueUserValueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,15 @@ public function it_passes_when_updating()
['email' => 'unique_user_value:123']
)->passes());
}

/** @test */
public function it_supports_overwriting_the_column()
{
User::make()->email('[email protected]')->save();

$this->assertTrue(Validator::make(
['baz' => '[email protected]'],
['baz' => 'unique_user_value:null,email']
)->fails());
}
}