Laravel unique validation on multiple columns
You can use Rule::unique
to achieve your validation rule
$messages = [
'data.ip.unique' => 'Given ip and hostname are not unique',
];
Validator::make($data, [
'data.ip' => [
'required',
Rule::unique('servers')->where(function ($query) use($ip,$hostname) {
return $query->where('ip', $ip)
->where('hostname', $hostname);
}),
],
],
$messages
);
edit: Fixed message assignation
Unique validation with 2 columns - Laravel 8.x
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.)
Rule::unique("account_users")->where(
function ($query) use ($request) {
return $query->where(
[
["account_id", "=", $request->account_id],
["extension", "=", $request->extension]
]
);
})->ignore(/* ID of currently edited row here. */)
CREATING A NEW RECORD.
Rule::unique("account_users")->where(
function ($query) use ($request) {
return $query->where(
[
["account_id", "=", $request->account_id],
["extension", "=", $request->extension]
]
);
})
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 theunique
method:
Rule::unique("account_users", "extension")->where(...)
Laravel Validation Unique Rule On Multiple Columns
Just make another rule:
$data = request()->validate([
'login' => ['required', 'string', 'email', 'max:255', 'unique:users,email','unique:users,phone'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
]);
Laravel unique validation of two columns when validating against an array
2022 Update
Laravel 9 now has functionality for this exact problem.
https://laravel.com/docs/9.x/validation#accessing-nested-array-data
Ended up with the following, though i was hoping there was a way to access the current index of the currently validated array element from within the function params themself.
$i = 0;
'email.*' => [
'required',
Rule::unique('users')->where(function ($query) use (&$i) {
return $query->where('account_id', $this->request('account_id')[$i]);
$i += 1;
})
]
How to validate combined uniqueness of two fields in Laravel 5.6?
$validationCondition = array(
...
'isd' => 'required',
'mobile' => 'required|unique:users,mobile,NULL,id,isd,' . $request->isd,
...
);
The meaning of that unique
rule for mobile
will be: "value of mobile
must be unique among all existing users that have isd
of the same value that came in request".
$request
here is the instance of incoming request that you are validating (obviously).
Laravel unique validation for combined multiple fields by custom logic
As you are asking sort hand of unique rule.
The (undocumented) format for the unique rule is:
table[,column[,ignore value[,ignore column[,where column,where value]...]]]
Note:: Multiple "where" conditions can be specified, but only equality can be checked. A closure (as in the accepted answer) is needed for any other comparisons.
But not recommended. Rather use closure for better options and readability.
Rule::unique('categories')
->where('parent_id', $parent_id)
->where(function ($sql) use ($request) {
$sql->where('name', $request->name)
->orWhere('slug', $request->name);
})
And separately handle the error messages
As far as I understand you are trying to handle the uniqueness of possible any slug->name, slug->slug, name->slug, name->name conversation, in that case, I would recommend using uid/UUID with a slug to prevent duplication.
Hope the answer helps
Related Topics
Tell Composer to Use Different PHP Version
Passing an Array to a Query Using a Where Clause
Api to Get All the Reviews and Rating from Google for Business
Count Same Values in Array and Combine into Array
How to Fix Warning Illegal String Offset in PHP
Undefined Index: User_Id. Where, User_Id Is Session Variable
Laravel Ajax Response Return HTML Elements
How to Increase Maximum Execution Time in Laravel
Check If a Specific Value Exists At a Specific Key in Any Subarray of a Multidimensional Array
How to Squeeze Error Message Out of Pdo
How to Get Username from Facebook Id
How to Find Out If CSV File Fields Are Tab Delimited or Comma Delimited
How to Get All Month Record Count in Laravel
I Have Three Columns, I Want to Combine Them All in One Main_Table
Creating Advanced Search Query With PHP Mysqli
Laravel 6: Call to Undefined Method App\\User::Createtoken()