ItSolutionStuff.com

Laravel Unique Validation on Multiple Columns Example

By Hardik Savani • April 16, 2024
Laravel

In this tutorial, i will show you laravel unique validation on multiple columns. Here you will learn laravel validation unique on two fields. This tutorial will give you simple example of laravel validation unique on two columns. you will learn laravel validation unique two columns update.

Here, i would like to show you how to use unique validation on multiple fields in laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 application. i will give you different way to adding unique validation with two column with your application. you can add unique validation at create time and also update time.

Sometime we need to add unique validation with multiple fields for username etc like evry company has unique username. at that time if you check unique email or username then you have to write database query manually and do it using if condition. but laravel provide "unique" rule that will help to easily add unique validation.

you can also follow basic tutorial from scratch here: Laravel Form Validation Example.

Basically, you can create your own request class using bellow command:

Create Request Class:

php artisan make:request StoreUserRequest

php artisan make:request UpdateUserRequest

Now you can use it, in your controller as like bellow:

Controller File Code:

<?php

namespace App\Http\Controllers;

use App\Http\Requests\StoreUserRequest;

use App\Http\Requests\UpdateUserRequest;

use App\User;

class UserController extends Controller

{

public function store(StoreUserRequest $request)

{

/* Do Something */

}

public function update(UpdateUserRequest $request, User $user)

{

/* Do Something */

}

}

Now you can see bellow solution one by one and you can use anyone that you require for unique validation.

Example 1: Store Unique Validation on Multiple Columns

app/Http/Requests/StoreUserRequest.php

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

use Illuminate\Validation\Rule;

class StoreUserRequest extends FormRequest

{

/**

* Determine if the user is authorized to make this request.

*

* @return bool

*/

public function authorize()

{

return true;

}

/**

* Get the validation rules that apply to the request.

*

* @return array

*/

public function rules()

{

return [

'name' => 'required',

'email' => 'required|email',

'username' => [

'required',

Rule::unique('users')

->where('company_id', $this->company_id)

]

];

}

}

Example 2: Update Unique Validation on Multiple Columns

app/Http/Requests/UpdateUserRequest.php

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class UpdateUserRequest extends FormRequest

{

/**

* Determine if the user is authorized to make this request.

*

* @return bool

*/

public function authorize()

{

return false;

}

/**

* Get the validation rules that apply to the request.

*

* @return array

*/

public function rules()

{

return [

'name' => 'required',

'email' => 'required|email',

'username' => [

'required',

Rule::unique('users')

->ignore($this->user)

->where('company_id', $this->company_id)

]

];

}

}

You can use anyone that you need.

I hope it can help you...

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Laravel Unique Validation on Update Example

Read Now →

How to Use Unique Validation in Laravel?

Read Now →

Special Characters Not Allowed Validation in Laravel

Read Now →

Laravel Form Validation Request Class Example

Read Now →

Space Not Allowed Validation in Laravel Example

Read Now →

Laravel Mobile/Phone Number Validation Example

Read Now →

Laravel Bail Rule | Stop Validation On First Failure

Read Now →

Laravel 7 Form Validation Tutorial

Read Now →

Laravel - Generate Captcha code and Validation example using BotDetect package

Read Now →

Laravel File Upload with Validation Example

Read Now →