Laravel - Validation - Rule Unique

The field under validation must not exist within the given database table.

Specifying A Custom Table / Column Name:

Instead of specifying the table name directly, you may specify the Eloquent model which should be used to determine the table name:

    
    'email' => 'unique:App\Models\User,email_address'
	

The column option may be used to specify the field's corresponding database column. If the column option is not specified, the name of the field under validation will be used.

    
    'email' => 'unique:users,email_address'
	

Specifying A Custom Database Connection

Occasionally, you may need to set a custom connection for database queries made by the Validator. To accomplish this, you may prepend the connection name to the table name:

    
    'email' => 'unique:connection.users,email_address'
	

Forcing A Unique Rule To Ignore A Given ID:

Sometimes, you may wish to ignore a given ID during unique validation. For example, consider an "update profile" screen that includes the user's name, email address, and location. You will probably want to verify that the email address is unique. However, if the user only changes the name field and not the email field, you do not want a validation error to be thrown because the user is already the owner of the email address in question.

To instruct the validator to ignore the user's ID, we'll use the Rule class to fluently define the rule. In this example, we'll also specify the validation rules as an array instead of using the | character to delimit the rules:

    
    use Illuminate\Support\Facades\Validator;
    use Illuminate\Validation\Rule;
    
    Validator::make($data, [
        'email' => [
            'required',
            Rule::unique('users')->ignore($user->id),
        ],
    ]);
	
You should never pass any user controlled request input into the ignore method. Instead, you should only pass a system generated unique ID such as an auto-incrementing ID or UUID from an Eloquent model instance. Otherwise, your application will be vulnerable to an SQL injection attack.

Instead of passing the model key's value to the ignore method, you may also pass the entire model instance. Laravel will automatically extract the key from the model:

    
    Rule::unique('users')->ignore($user)
	

If your table uses a primary key column name other than id, you may specify the name of the column when calling the ignore method:

    
    Rule::unique('users')->ignore($user->id, 'user_id')
	

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:

    
    Rule::unique('users', 'email_address')->ignore($user->id),
	

Adding Additional Where Clauses:

You may specify additional query conditions by customizing the query using the where method. For example, let's add a query condition that scopes the query to only search records that have an account_id column value of 1:

    
    'email' => Rule::unique('users')->where(function ($query) {
        return $query->where('account_id', 1);
    })