Today in this post we will see laravel unique validation on update. unique validation rule in laravel is used when you want to validate any email id or mobile number.
Here, we will see laravel unique email validation on update form or laravel unique validation with condition, laravel provide "unique" rule for email unique validation in laravel. So, let's see how to add unique validation on update in laravel
Here, you can create request class for email unique validation in laravel, run below command to create request class.
php artisan make:request StoreUserRequest
php artisan make:request UpdateUserRequest
Your controller file look like below.
<?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)
{
//Your Code
}
public function update(UpdateUserRequest $request, User $user)
{
//Your Code
}
}
Add below code in this file app/Http/Requests/StoreUserRequest.php
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class StoreUserRequest extends FormRequest
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'name' => 'required',
'username' => 'required|min:8',
'email' => 'required|email|unique:users,email'
];
}
}
Add below code in this file app/Http/Requests/StoreUserRequest.php
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class StoreUserRequest extends FormRequest
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'name' => 'required',
'username' => 'required|min:8',
'email' => 'required|email|unique:users'
];
}
}
Add below code in this file app/Http/Requests/StoreUserRequest.php
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
class StoreUserRequest extends FormRequest
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'name' => 'required',
'username' => 'required|min:8',
'email' => ['required', Rule::unique('users')]
];
}
}
Add below code in this file app/Http/Requests/UpdateUserRequest.php
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class UpdateUserRequest extends FormRequest
{
public function authorize()
{
return false;
}
public function rules()
{
return [
'name' => 'required',
'username' => 'required|min:8',
'email' => 'required|email|unique:users,email,'.$this->user->id
];
}
}
Add below code in this file app/Http/Requests/UpdateUserRequest.php
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class UpdateUserRequest extends FormRequest
{
public function authorize()
{
return false;
}
public function rules()
{
return [
'name' => 'required',
'username' => 'required|min:8',
'email' => ['required', Rule::unique('users')->ignore($this->user)]
];
}
}
You may also like :
In this article, we will see how to toggle between dark and light modes using jquery. As per the current trend of web de...
Nov-24-2020
In this article, we will see laravel 9 one to one relationship example. Also, you can use one to one re...
Apr-01-2022
In this tutorial I will show you how to download youtube video using jquery or how to download youtube video from s...
Sep-27-2021
If you're a developer, you're likely to have frustration with "index.php" cluttering up your website...
Jan-13-2023