Skip to content

Conversation

@Ferks-FK
Copy link

This PR adds support for the unique rule.
Currently validations that use the unique rule were ignored.

With this PR:

    public function rules(): array
    {
        return [
            'code' => ['required', 'string', 'alpha_dash', 'min:4', 'max:36',
                Rule::unique('vouchers')->ignore($this->route('voucher')?->id),
            ],
        ];
    }
image

Without (same rules):
image

Note: I tried to make $this->route(‘voucher’) resolvable, so that the description would be:

Must be unique in the <code>vouchers</code> table for column <code>id</code> (ignoring record with <code>id</code> = <code>5</code>).

This is useful for update cases where we need to ignore an ID.
However, I couldn't get it to work and I don't even know if it's possible, let me know.

Copy link
Contributor

@shalvah shalvah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the work! You need to check the tests, though.

On a conceptual level, I'm not convinced about this feature-I've had some people complain about any references to a database table. 🤔 I think there's an argument there, that this is an implementation detail we shouldn't mention in the API. The more I think about it, the more I think it's better to just say "Must not be taken".

$table = $ruleArguments[0] ?? 'table';
$column = isset($ruleArguments[1])
? ($ruleArguments[1] === 'NULL' ? 'id' : $ruleArguments[1])
: 'id'; // If the column is not specified, assume it is 'id'.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think doing this may cause some issues for people with some other column. Can we change this to not mention a column instead?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As it stands, it does not require the column to be provided.
Rule::unique('vouchers') It is the equivalent Rule::unique('vouchers', 'id')
The ID column is assumed, because Laravel assumes that it is also ID.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's just go with "Must not be taken" (which is Rails' default unique validation message). Database table and column names are implementation details that API consumers should neither know or care about.

Copy link
Contributor

@shalvah shalvah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's go with "Must not be taken".

@Ferks-FK Ferks-FK requested a review from shalvah August 2, 2025 12:28
Copy link
Contributor

@shalvah shalvah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I still think we should not be mentioning the table name. We had something like that before, but there were complaints that we were exposing an implementation detail to API consumers. And that makes sense: if the endpoint is POST /suppliers, but you store in a table called third_parties (for instance, if you do Single Table Inheritance), returning an error message, Must be unique in the "third_parties" table would be confusing.

Sorry it's taken me so long to get back to you on this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants