All Projects → felixkiss → Uniquewith Validator

felixkiss / Uniquewith Validator

Licence: mit
Custom Laravel Validator for combined unique indexes

Projects that are alternatives of or similar to Uniquewith Validator

Ardent
Self-validating, secure and smart models for Laravel's Eloquent ORM
Stars: ✭ 1,412 (+301.14%)
Mutual labels:  database, laravel
Config
Manage Laravel configuration by persistent storage
Stars: ✭ 139 (-60.51%)
Mutual labels:  database, laravel
Laravel Settings
Store key value pair in database as settings
Stars: ✭ 107 (-69.6%)
Mutual labels:  database, laravel
Db Seeder
A database seeder app for MySQL
Stars: ✭ 77 (-78.12%)
Mutual labels:  database, laravel
Laravel Migrate Fresh
An artisan command to build up a database from scratch
Stars: ✭ 179 (-49.15%)
Mutual labels:  database, laravel
Laravel Sync Migration
Developer tool helps to sync migrations without refreshing the database
Stars: ✭ 89 (-74.72%)
Mutual labels:  database, laravel
Laravel Database Schedule
Manage your Laravel Task Scheduling in a friendly interface and save schedules to the database.
Stars: ✭ 94 (-73.3%)
Mutual labels:  database, laravel
Backup
MySQL Database backup package for Laravel
Stars: ✭ 66 (-81.25%)
Mutual labels:  database, laravel
Laravel Translatable
[Deprecated] A Laravel package for multilingual models
Stars: ✭ 1,974 (+460.8%)
Mutual labels:  database, laravel
Laravel Db Profiler
Database Profiler for Laravel Web and Console Applications.
Stars: ✭ 141 (-59.94%)
Mutual labels:  database, laravel
Laravel Log To Db
Custom Laravel and Lumen 5.6+ Log channel handler that can store log events to SQL or MongoDB databases. Uses Laravel/Monolog native logging functionality.
Stars: ✭ 76 (-78.41%)
Mutual labels:  database, laravel
Laravel World
provide countries, states, and cities relations and database.
Stars: ✭ 222 (-36.93%)
Mutual labels:  database, laravel
Laravel Db Normalizer
Normalize all database results to one unified interface, to make swapping repositories a breeze.
Stars: ✭ 72 (-79.55%)
Mutual labels:  database, laravel
Lara Lens
Laravel package for display diagnostic (config, database, http connections...)
Stars: ✭ 96 (-72.73%)
Mutual labels:  database, laravel
Laravel Optimistic Locking
Adds optimistic locking feature to eloquent models.
Stars: ✭ 71 (-79.83%)
Mutual labels:  database, laravel
Backup Manager
Database backup manager for dumping to and restoring databases from S3, Dropbox, FTP, SFTP, and Rackspace Cloud
Stars: ✭ 1,589 (+351.42%)
Mutual labels:  database, laravel
Laravel Cadillac
🍺 A database tool for laravel.
Stars: ✭ 63 (-82.1%)
Mutual labels:  database, laravel
Prequel
Prequel for Laravel. Clear and concise database management.
Stars: ✭ 1,141 (+224.15%)
Mutual labels:  database, laravel
Laravel Scout Postgres
PostgreSQL Full Text Search Engine for Laravel Scout
Stars: ✭ 140 (-60.23%)
Mutual labels:  database, laravel
Townhouse
A multi-tenant Laravel app for listing property rentals
Stars: ✭ 218 (-38.07%)
Mutual labels:  database, laravel

unique_with Validator Rule For Laravel

Build Status

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

Documentation for older versions

Installation

Install the package through Composer. On the command line:

composer require felixkiss/uniquewith-validator

Configuration

Add the following to your providers array in config/app.php:

'providers' => [
    // ...

    Felixkiss\UniqueWithValidator\ServiceProvider::class,
],

Usage

Use it like any Validator rule:

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

See the Validation documentation of Laravel.

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)

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)

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:

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.:

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:

$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:

<?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:

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].