Skip to content
Advertisement

Unique validation with 2 columns – Laravel 8.x

I’ve been trying make my validation so that an extension must be unique to it’s own company but not to other companies. Here is my DB table:

JavaScript

And my validation rule looks like this:

JavaScript

But still I got errors saying that the extension number is already taken. It seems that the Unique rule doesn’t accept multiple columns, only one. Is this true?

How can I fix this?

Advertisement

Answer

Forcing A Unique Rule To Ignore A Given ID:

UPDATING AN EXISTING RECORD.

“account_users” => Table name.

“account_id”, “extension” => The 2 fields to check for uniqueness.

ID of currently edited row here. => The id (primary key) to ignore. (The currently updated/edited table row id.)

JavaScript

CREATING A NEW RECORD.

JavaScript

Addendum

By default, the unique rule will check the uniqueness of the column matching the name of the attribute being validated. However, you may pass a different column name as the second argument to the unique method:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement