Laravel unique validation on multiple columns

Laravel unique validation on multiple columns

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Laravel Unique Validation on Multiple Columns

You are trying to enforce a uniqueness constraint where a combination of an IP address and a hostname must be unique within your servers table. Your current validation rule checks only the ip column:

'data.ip' => ['required', 'unique:servers,ip,' . $this->id]

This approach fails to cover your specific business logic because it ignores the hostname. If a user enters an existing IP that is already paired with a different hostname on another server, Laravel will allow the duplicate entry. To fix this, you need to validate against the composite key formed by both columns.

Laravel's built-in unique validation rule supports checking multiple columns, but there is a critical architectural requirement you must satisfy first: the database schema itself.

The Database Constraint is Mandatory

You cannot rely solely on application-layer validation for uniqueness if the database does not enforce it. While Laravel can prevent duplicates during the current request, it cannot guarantee data integrity across concurrent requests or when other parts of your application write to the table.

To make the multi-column check work reliably, you must define a unique index (or constraint) in your database that encompasses both columns. This ensures that even if validation logic is bypassed or fails for some reason, the database will reject the insert or update.

Using Laravel Migrations, you would add a unique index to your servers table like this:

Schema::create('servers', function (Blueprint $table) {
    $table->id();
    $table->string('ip');
    $table->string('hostname');
    $table->timestamps();

    // Enforce uniqueness on the combination of IP and Hostname
    $table->unique(['ip', 'hostname']);
});

Once this index exists, your application validation becomes a safety net rather than the sole source of truth. You can now configure Laravel to check against both columns in a single rule.

Implementing Multi-Column Validation

When you need to validate existing data (where data.ip might already be present in the database), you typically use the unique rule with the column names and an optional ID for exclusion. To check both columns, pass them as an array of column names.

Here is how you should update your validation logic:

'data.ip' => [
    'required', 
    'unique:servers,ip,hostname,' . $this->id
]

In this example:

  • servers is the table name.
  • ip,hostname are the columns to check against. Note that they must be listed in the same order as they appear in your unique database index.
  • $this->id is passed as an optional value to exclude the current row from the check if you are updating an existing record (though usually, this applies when validating ip and hostname fields directly on the model).

If you are creating a new server via a request where no ID exists yet, omit the exclusion parameter:

'data.ip' => [
    'required', 
    'unique:servers,ip,hostname'
]

Handling Composite Unique Constraints in Form Requests

Often, multi-column validation is handled inside a Form Request class rather than directly on the controller. If you are using a Form Request, ensure your $rules property reflects the composite key logic.

public function rules()
{
    return [
        'ip' => ['required', 'string', 'max:45'],
        'hostname' => ['required', 'string', 'max:255'],
        
        // Check uniqueness against both columns, excluding the current record if updating
        'ip' => $this->unique('servers', 'ip', 'hostname', $this->id),
        'hostname' => $this->unique('servers', 'hostname', 'ip', $this->id),
    ];
}

Note: The order of columns in the validation rule (ip, hostname vs hostname, ip) must match the order defined in your database unique index. Laravel validates against the primary column first, then subsequent columns.

Why You Should Not Rely on Validation Alone

It is tempting to think that if the validation passes, the data is safe. However, validation is stateless regarding concurrent requests. If two users submit a form with the same IP and Hostname combination at the exact same time:

  1. User A's request hits the database first; the row is inserted successfully.
  2. User B's request hits the database second; the insertion fails because the unique index now exists.

This race condition will cause a database exception (SQLSTATE[23000]... Duplicate entry). While you can catch this and return an error message, it is far better to let the database handle the hard constraint. The validation rule serves as immediate feedback for the user ("You already have a server with this IP/Hostname") and prevents unnecessary database hits, but the schema must hold the final authority.

For more advanced scenarios involving complex form handling or custom validation logic, you might explore additional resources on building robust Laravel forms at https://laravelcompany.com. However, for standard uniqueness constraints, adhering to the database index pattern described above is the most performant and maintainable solution.

Our network