2017 © Pedro Peláez
 

library uniquewith-validator

Custom Laravel Validator for combined unique indexes

image

felixkiss/uniquewith-validator

Custom Laravel Validator for combined unique indexes

  • Monday, August 7, 2017
  • by felixkiss
  • Repository
  • 15 Watchers
  • 250 Stars
  • 431,536 Installations
  • PHP
  • 15 Dependents
  • 0 Suggesters
  • 54 Forks
  • 13 Open issues
  • 23 Versions
  • 9 % Grown

The README.md

unique_with Validator Rule For Laravel

Build Status, (*1)

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

Documentation for older versions

Installation

Install the package through Composer. On the command line:, (*3)

composer require felixkiss/uniquewith-validator

Configuration

Add the following to your providers array in config/app.php:, (*4)

'providers' => [
    // ...

    Felixkiss\UniqueWithValidator\ServiceProvider::class,
],

Usage

Use it like any Validator rule:, (*5)

$rules = [
    '<field1>' => 'unique_with:<table>,<field2>[,<field3>,...,<ignore_rowid>]',
];

See the Validation documentation of Laravel., (*6)

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., (*7)

e.g. your input contains a field 'last_name', but the column in your database is called 'sur_name':, (*8)

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

Ignore existing row (useful when updating)

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

This will ignore row with id 2, (*10)

$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, (*11)

$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, (*12)

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

Add additional clauses (e.g. 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, (*13)

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

Soft delete caveat:, (*14)

In Laravel 5 (tested on 5.5), if the validation is performed in form request class, field deleted_at is skipped, because it's not send in request. To solve this problem, add 'deleted_at' => null to Your validation parameters in request class., e.g.:, (*15)

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

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:, (*16)

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

Example

Pretend you have a users table in your database plus User model like this:, (*17)

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;

class CreateUsersTable extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function(Blueprint $table) {
            $table->increments('id');

            $table->timestamps();

            $table->string('first_name');
            $table->string('last_name');

            $table->unique(['first_name', 'last_name']);
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('users');
    }

}
<?php

class User extends Eloquent { }

Now you can validate a given first_name, last_name combination with something like this:, (*18)

Route::post('test', function() {
    $rules = [
        'first_name' => 'required|unique_with:users,last_name',
        'last_name' => 'required',
    ];

    $validator = Validator::make(Input::all(), $rules);

    if($validator->fails()) {
        return Redirect::back()->withErrors($validator);
    }

    $user = new User;
    $user->first_name = Input::get('first_name');
    $user->last_name = Input::get('last_name');
    $user->save();

    return Redirect::home()->with('success', 'User created!');
});

License

MIT, (*19)

The Versions

07/08 2017

dev-master

9999999-dev https://github.com/felixkiss/uniquewith-validator

Custom Laravel Validator for combined unique indexes

  Sources   Download

MIT

The Requires

 

The Development Requires

by Felix Kiss

laravel

07/08 2017

3.1.2

3.1.2.0 https://github.com/felixkiss/uniquewith-validator

Custom Laravel Validator for combined unique indexes

  Sources   Download

MIT

The Requires

 

The Development Requires

by Felix Kiss

laravel

12/05 2017

3.1.1

3.1.1.0 https://github.com/felixkiss/uniquewith-validator

Custom Laravel Validator for combined unique indexes

  Sources   Download

MIT

The Requires

 

The Development Requires

by Felix Kiss

laravel

03/05 2017

3.1.0

3.1.0.0 https://github.com/felixkiss/uniquewith-validator

Custom Laravel Validator for combined unique indexes

  Sources   Download

MIT

The Requires

 

The Development Requires

by Felix Kiss

laravel

17/02 2017

dev-dot-validation

dev-dot-validation https://github.com/felixkiss/uniquewith-validator

Custom Laravel Validator for combined unique indexes

  Sources   Download

MIT

The Requires

 

The Development Requires

by Felix Kiss

laravel

17/02 2017

3.0.0

3.0.0.0 https://github.com/felixkiss/uniquewith-validator

Custom Laravel Validator for combined unique indexes

  Sources   Download

MIT

The Requires

 

The Development Requires

by Felix Kiss

laravel

14/02 2017

2.0.8

2.0.8.0 https://github.com/felixkiss/uniquewith-validator

Custom Laravel Validator for combined unique indexes

  Sources   Download

MIT

The Requires

 

The Development Requires

by Felix Kiss

laravel

11/02 2016

2.0.7

2.0.7.0 https://github.com/felixkiss/uniquewith-validator

Custom Laravel Validator for combined unique indexes

  Sources   Download

MIT

The Requires

 

The Development Requires

by Felix Kiss

laravel

25/11 2015

2.0.6

2.0.6.0 https://github.com/felixkiss/uniquewith-validator

Custom Laravel Validator for combined unique indexes

  Sources   Download

MIT

The Requires

 

The Development Requires

by Felix Kiss

laravel

14/10 2015

2.0.5

2.0.5.0 https://github.com/felixkiss/uniquewith-validator

Custom Laravel Validator for combined unique indexes

  Sources   Download

MIT

The Requires

 

The Development Requires

by Felix Kiss

laravel

06/09 2015

2.0.4

2.0.4.0 https://github.com/felixkiss/uniquewith-validator

Custom Laravel Validator for combined unique indexes

  Sources   Download

MIT

The Requires

 

The Development Requires

by Felix Kiss

laravel

05/08 2015

2.0.3

2.0.3.0 https://github.com/felixkiss/uniquewith-validator

Custom Laravel Validator for combined unique indexes

  Sources   Download

MIT

The Requires

 

The Development Requires

by Felix Kiss

laravel

17/06 2015

2.0.2

2.0.2.0 https://github.com/felixkiss/uniquewith-validator

Custom Laravel Validator for combined unique indexes

  Sources   Download

MIT

The Requires

 

The Development Requires

by Felix Kiss

laravel

30/03 2015

2.0.1

2.0.1.0 https://github.com/felixkiss/uniquewith-validator

Custom Laravel Validator for combined unique indexes

  Sources   Download

MIT

The Requires

 

The Development Requires

by Felix Kiss

laravel

18/02 2015

2.0.0

2.0.0.0 https://github.com/felixkiss/uniquewith-validator

Custom Laravel Validator for combined unique indexes

  Sources   Download

MIT

The Requires

 

The Development Requires

by Felix Kiss

laravel

05/01 2015

1.1.4

1.1.4.0 https://github.com/felixkiss/uniquewith-validator

Custom Laravel 4 Validator for combined unique indexes

  Sources   Download

MIT

The Requires

 

The Development Requires

by Felix Kiss

laravel

29/12 2014

1.1.3

1.1.3.0 https://github.com/felixkiss/uniquewith-validator

Custom Laravel 4 Validator for combined unique indexes

  Sources   Download

MIT

The Requires

 

The Development Requires

by Felix Kiss

laravel

07/08 2014

1.1.2

1.1.2.0 https://github.com/felixkiss/uniquewith-validator

Custom Laravel 4 Validator for combined unique indexes

  Sources   Download

MIT

The Requires

 

The Development Requires

by Felix Kiss

laravel

25/05 2014

1.1.1

1.1.1.0 https://github.com/felixkiss/uniquewith-validator

Custom Laravel 4 Validator for combined unique indexes

  Sources   Download

MIT

The Requires

 

The Development Requires

by Felix Kiss

laravel

19/05 2014

1.1.0

1.1.0.0 https://github.com/felixkiss/uniquewith-validator

Custom Laravel 4 Validator for combined unique indexes

  Sources   Download

MIT

The Requires

 

The Development Requires

by Felix Kiss

laravel

10/03 2014

1.0.2

1.0.2.0 https://github.com/felixkiss/uniquewith-validator

Custom Laravel 4 Validator for combined unique indexes

  Sources   Download

MIT

The Requires

 

The Development Requires

by Felix Kiss

laravel

02/01 2014

1.0.1

1.0.1.0 https://github.com/felixkiss/uniquewith-validator

Custom Laravel 4 Validator for combined unique indexes

  Sources   Download

MIT

The Requires

 

The Development Requires

by Felix Kiss

laravel

02/01 2014

1.0.0

1.0.0.0 https://github.com/felixkiss/uniquewith-validator

Custom Laravel 4 Validator for combined unique indexes

  Sources   Download

MIT

The Requires

 

The Development Requires

by Felix Kiss

laravel