Laravel unique validation on multiple columns
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Title: Laravel Unique Validation Across Multiple Columns - A Comprehensive Guide
Introduction: In a modern web application, your database schema is critical to ensuring reliability and efficiency. Sometimes, you might have multiple columns in a table that need unique values across the entire table or a specific subset. In Laravel, validating these special scenarios can be quite challenging but not impossible with the right approach. This article will delve deep into how to implement effective validation on multiple columns in Laravel.
1. Understanding Laravel's Built-in Validation Rules: Laravel boasts an extensive set of built-in validation rules that cater to most common use cases. Among these are 'required', 'unique', and 'exists'. The 'unique' rule allows you to check if a value is unique within the specified table or column, while the 'exists' rule checks for the existence of a given relationship.
2. Validation Rules in Laravel: In Laravel, validation rules are defined within your models using the validate() method or the Request class. The following example shows how to apply 'unique' validation on the 'ip' and 'hostname' columns based on the previous code snippet:
static function rules() {
return [
'data.ip' => ['required', 'unique:servers,ip,'.$this->id],
'data.hostname' => ['required', 'unique:servers,hostname,'.$this->id]
];
}
In this example, the data validation rules are defined in a static function called rules() within your model. The 'ip' column is validated as unique across the 'servers' table based on its own column and with the condition of excluding the current record's id. Similarly, the 'hostname' column has its own validation rule, also excluding the current row's id.
3. Custom Validation Rules: Sometimes, built-in rules do not cater to specific use cases, requiring you to create your own custom validation rules. To do so in Laravel, simply extend the Validator class and create a new validation method with the required parameters. For example, let's assume we want to validate the combination of 'ip' and 'hostname' being unique across other records within the servers table:
class CustomValidator extends Validator {
// ...
public function validateCombinations($attribute, $value, $fail) {
$constraints = [
['column1' => 'ip', 'constraint' => 'servers.ip'],
['column2' => 'hostname', 'constraint' => 'servers.hostname']
];
foreach ($constraints as $constraint) {
if (!self::validateCombination($value, $constraint['column1'], $constraint['column2'])) {
return $fail('The :attribute must have a unique combination of IP and hostname.');
}
}
}
}
In this example, we've created a new method called validateCombinations() which takes the attribute name, value, and a callback function. This method fetches the constraints array containing the column names and corresponding table/column pairs to be validated. We then iterate through these constraints and call another custom method (which is not shown here) that validates the combinations using the provided ip and hostname values.
4. Using Fluent Validation: Another alternative to defining validation rules within a model or Request class is by using Laravel's fluent validation. This allows you to define your validation logic as a separate file, making it easier to maintain and manage. To apply unique validation on multiple columns with this approach, create a new validation class that extends the Validator class:
class MyValidator extends \Illuminate\Validation\Validator {
// ...
}
Now, define your validation rules within this class using the addRule() method and apply it on your model or Request class as required. For example:
$validator = Validator::make($request->all(), [
'data.ip' => ['required', new CustomCombinationValidator('servers', 'ip', 'hostname')],
]);
In this code snippet, we're using the validateCombinations() method defined earlier in our custom validator class to perform the combination validation on both ip and hostname columns. This further simplifies the process of implementing custom validation rules for multiple columns involving unique combinations.
Conclusion: Laravel provides numerous options to implement effective validation across multiple columns. From using built-in validation rules, creating custom validation methods, or leveraging fluent validation classes, you can ensure that your database is well-maintained and protected against duplicated values in specific columns. With the proper implementation, you can easily handle complex validation scenarios and maintain a robust Laravel application.