2017 © Pedro Peláez
 

library uniquewith-validator

Custom Laravel Validator for combined unique indexes

image

eastree/uniquewith-validator

Custom Laravel Validator for combined unique indexes

  • Thursday, April 26, 2018
  • by eastree
  • Repository
  • 0 Watchers
  • 0 Stars
  • 25 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 54 Forks
  • 0 Open issues
  • 23 Versions
  • 0 % Grown

The README.md

unique_with Validator Rule For Laravel

Build Status Scrutinizer Code Quality, (*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',
];

Example

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

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

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!');
});

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

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

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

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

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

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

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

License

MIT, (*16)

The Versions

26/04 2018

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 validator unique with

26/04 2018

3.1.3

3.1.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 validator unique with

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

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