Skip to main content

Overview

The Uniqueness validation rules provide specialized validation for many-to-many relationships, bulk operations, and composite uniqueness constraints. These rules handle complex scenarios that Laravel’s built-in unique rule cannot address.

UniqueInPivot

Validates that a column value is unique within the scope of a many-to-many relationship by checking through a pivot table. Designed for single operations (create/update).

Constructor Parameters

string
required
Database connection name
string
required
Main table containing the column to validate (e.g., 'addresses')
string
required
Pivot table that links main table to owner (e.g., 'user_addresses')
string
required
Foreign key in pivot table pointing to main table (e.g., 'address_id')
string
required
Foreign key in pivot table pointing to owner (e.g., 'user_id')
mixed
required
Owner ID value (e.g., auth()->id())
string
required
Column in main table to check for uniqueness (e.g., 'phone')
string
default:"id"
Primary key of main table
mixed
default:"null"
Primary key value to ignore (for updates: $this->route('id'))

Create Example

SQL Executed:

Update Example

SQL Executed:

Schema Support

UniqueInPivotArray

Validates column uniqueness within a many-to-many relationship for bulk (array) operations. Checks both intra-array duplicates and database conflicts.

Constructor Parameters

string
required
Database connection name
string
required
Main table containing the column to validate
string
required
Pivot table that links main table to owner
string
required
Foreign key in pivot table pointing to main table
string
required
Foreign key in pivot table pointing to owner
mixed
required
Owner ID value
string
required
Column to check for uniqueness
string
required
Root array key in request data (e.g., 'addresses')
string
default:"id"
Primary key of main table
string|null
default:"null"
Field containing primary key to ignore (for updates: 'id')

Bulk Create Example

Request:
Error:

Bulk Update Example

Valid Request:
Invalid Request:
Error:

UniqueCompositeInArray

Validates uniqueness of a column in a database table for bulk operations, supporting composite conditions. Simpler than UniqueInPivotArray when pivot tables aren’t involved.

Constructor Parameters

string
required
Database connection name
string
required
Table name to check
string
required
Column to check for uniqueness
string
required
Root array key in request data
array
default:"[]"
Additional WHERE conditions (composite key)
string|null
default:"null"
Field containing primary key to ignore (for updates)

Bulk Create Example

Request:

Bulk Update Example

Multi-Tenant Example

ArrayCount

Validates that an array contains between min and max elements, with support for custom messages and token replacement.

Constructor Parameters

int|null
default:"null"
Minimum number of items (inclusive)
int|null
default:"null"
Maximum number of items (inclusive)
string|null
default:"null"
Single custom message for all failure scenarios
array
default:"[]"
Per-scenario messages: onMin, onMax, onBetween, onExact, onNotArray

Basic Usage

Custom Global Message

Per-Scenario Messages

Token Replacement

Available tokens:
  • :attribute - Field name (e.g., “addresses”)
  • :min - Minimum value
  • :max - Maximum value
  • :count - Actual count provided

Exact Count

Only Minimum

Only Maximum

Complete Example

See Also