Laravel request update unique field only if the change
You want to add unique validation if email changed, if that is the case then you just need to add a condition validation.
Here the example is main you can create a user or one time use email is another user to not provide and update time same provider but currently update user email id is old working and new working but another user use its email id.
The validation will look like this when you will create a new record $user_id will be null but at the time of update it will have value. So this validation will work for both create and update.
Syntax :
'field_name' => 'required|unique:table_name,field_name,'. $id.',id',
Example :
use Illuminate\Http\Request; use App\User; public function login(Request $request) { $user_id = isset($users) ? $users->id : null; $validator = Validator::make($request->all(), [ 'email' => 'required|email|unique:users,email,'. $user_id.',id', ]); if ($validator->fails()) { return response()->json(['error'=>$validator->errors()], 401); } ... }