skip to Main Content

I want to implement unique validation on one column but it should only check active rows of the table.
so is there any way I can implement this logic using laravel validation?

Below is the logic I am using to check unique records but its not working.

unique:users,email,is_active,1

2

Answers


  1. You can scope the query with additional clauses like that:

    Rule::unique('users', 'email')->where(fn ($query) => $query->where('is_active', 1));
    

    Another syntax is:

    unique:users,email,NULL,id,is_active,1
    
    Login or Signup to reply.
  2. According to the laravel validation document, you can do it as:

    Rule::unique('users', 'email')->ignore(0, "is_active"),
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search