In the query class, you probably need this check in the PUT or PATCH method, where you don't have a user, then you can just use this rule
You have 2 options to do this
1
'email' => "unique:users,email,$this->id,id"
OR
2:
use Illuminate\Validation\Rule; //import Rule class 'email' => Rule::unique('users')->ignore($this->id); //use it in PUT or PATCH method
$ this-> identifier provides the user identifier, since $ this is an object of the request class and the request also contains the user object.
public function rules() { switch ($this->method()) { case 'POST': { return [ 'name' => 'required', 'email' => 'required|email|unique:users', 'password' => 'required' ]; } case 'PUT': case 'PATCH': { return [ 'name' => 'required', 'email' => "unique:users,email,$this->id,id", OR
Hope this solves the problem when using the query class.
Afraz source share