PHPackages                             reinbier/laravel-unique-with - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. reinbier/laravel-unique-with

ActiveLibrary

reinbier/laravel-unique-with
============================

Unique With Validator rule for Laravel

v2.0.0(1y ago)996.2k↓16.4%2[4 PRs](https://github.com/Reinbier/laravel-unique-with/pulls)MITPHPPHP ^8.2CI passing

Since Mar 30Pushed 2mo ago1 watchersCompare

[ Source](https://github.com/Reinbier/laravel-unique-with)[ Packagist](https://packagist.org/packages/reinbier/laravel-unique-with)[ Docs](https://github.com/reinbier/laravel-unique-with)[ RSS](/packages/reinbier-laravel-unique-with/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (5)Dependencies (14)Versions (10)Used By (0)

Unique With Validator rule for Laravel
======================================

[](#unique-with-validator-rule-for-laravel)

[![Latest Version on Packagist](https://camo.githubusercontent.com/acd5c80203dced23ddd2c0c43a5b3518d7a5bda598fae9c8ec249c47671649e7/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7265696e626965722f6c61726176656c2d756e697175652d776974682e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/reinbier/laravel-unique-with)[![GitHub Tests Action Status](https://camo.githubusercontent.com/454bf16b757ca6dd0fd687262462d8b9620f9e9139da810bf4818a7f6d0eb35c/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f7265696e626965722f6c61726176656c2d756e697175652d776974682f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/reinbier/laravel-unique-with/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/1f0eeec264e7628d0e13cac8c84847b063f0c437cc5a4e0f459272b2ef79111a/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f7265696e626965722f6c61726176656c2d756e697175652d776974682f6669782d7068702d636f64652d7374796c652d6973737565732e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/reinbier/laravel-unique-with/actions?query=workflow%3A%22Fix+PHP+code+style+issues%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/6deccd0f0c3b1039ed8218d802b583ffb4f220e0aeab9e73e726c90e087e229c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7265696e626965722f6c61726176656c2d756e697175652d776974682e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/reinbier/laravel-unique-with)

This package contains a variant of the validateUnique rule for Laravel, that allows for validation of multi-column UNIQUE indexes.

#### Please note

[](#please-note)

*This package is to continue development of the package [uniquewith-validator](https://github.com/felixkiss/uniquewith-validator) by [Felix Kiss](https://github.com/felixkiss) and have it work with recent versions of the Laravel framework (continuing from Laravel 9 onwards). For older versions of the framework, please use the aforementioned package.*

### Versioning

[](#versioning)

For the sake of maintainability and testability, this package uses different major versions to support Laravel, make sure to pick the right one based on the table below.

Laravel versionPackage versionv9 &amp; 10v1.xv11 &amp; 12v2.xInstallation
------------

[](#installation)

You can install the package via composer:

```
composer require reinbier/laravel-unique-with
```

Usage
-----

[](#usage)

Use it like any `Validator` rule:

```
$rules = [
    '' => 'unique_with:,[,,...,]',
];
```

See the [Validation documentation](http://laravel.com/docs/validation) of Laravel.

### Specify different column names in the database

[](#specify-different-column-names-in-the-database)

If your input field names are different from the corresponding database columns, you can specify the column names explicitly.

e.g. your input contains a field 'last\_name', but the column in your database is called 'sur\_name':

```
$rules = [
    'first_name' => 'unique_with:users, middle_name, last_name = sur_name',
];
```

### Ignore existing row (useful when updating)

[](#ignore-existing-row-useful-when-updating)

You can also specify a row id to ignore (useful to solve unique constraint when updating)

This will ignore row with id 2

```
$rules = [
    'first_name' => 'required|unique_with:users,last_name,2',
    'last_name' => 'required',
];
```

To specify a custom column name for the id, pass it like

```
$rules = [
    'first_name' => 'required|unique_with:users,last_name,2 = custom_id_column',
    'last_name' => 'required',
];
```

If your id is not numeric, you can tell the validator

```
$rules = [
    'first_name' => 'required|unique_with:users,last_name,ignore:abc123',
    'last_name' => 'required',
];
```

### Add additional clauses (e.g. when using soft deletes)

[](#add-additional-clauses-eg-when-using-soft-deletes)

You can also set additional clauses. For example, if your model uses soft deleting then you can use the following code to select all existing rows but marked as deleted

```
$rules = [
    'first_name' => 'required|unique_with:users,last_name,deleted_at,2 = custom_id_column',
    'last_name' => 'required',
];
```

*Soft delete caveat:*

If the validation is performed in a form request class, field deleted\_at is skipped, because it's not send in request. To solve this problem, add 'deleted\_at' =&gt; null to your validation parameters in request class., e.g.:

```
protected function validationData()
{
    return array_merge($this->request->all(), [
        'deleted_at' => null
    ]);
}
```

### Specify specific database connection to use

[](#specify-specific-database-connection-to-use)

If we have a connection named `some-database`, we can enforce this connection (rather than the default) like this:

```
$rules = [
    'first_name' => 'unique_with:some-database.users, middle_name, last_name',
];
```

Example
-------

[](#example)

Pretend you have a `users` table in your database plus `User` model like this:

```
