Laravel Unique Validation on Update Example

By Hardik Savani April 16, 2024 Category : Laravel

This simple article demonstrates of laravel unique validation on update. This post will give you simple example of laravel form request validation unique update. it's simple example of laravel validation unique ignore id. i explained simply about laravel validation unique on update.

Here, i would like to show you how to add unique validation on update in laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 application. i will give you different way to adding unique field validation on your application. you can add unique validation on update request class.

Sometime we need to add unique validation on update for email, username etc. 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: Simple Unique Validation

app/Http/Requests/StoreUserRequest.php

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

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',

'username' => 'required|min:8',

'email' => 'required|email|unique:users'

];

}

}

Example 2: Unique Validation with Column Name

app/Http/Requests/StoreUserRequest.php

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

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',

'username' => 'required|min:8',

'email' => 'required|email|unique:users,email'

];

}

}

Example 3: Unique Validation with Rule

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',

'username' => 'required|min:8',

'email' => ['required', Rule::unique('users')]

];

}

}

Example 4: Unique Validation with Update

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',

'username' => 'required|min:8',

'email' => 'required|email|unique:users,email,'.$this->user->id

];

}

}

Example 5: Unique Validation with Update Rule

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',

'username' => 'required|min:8',

'email' => ['required', Rule::unique('users')->ignore($this->user)]

];

}

}

You can use anyone that you need.

I hope it can help you...

Shares