Search code examples
phplaravelvalidationrequestlumen

lumen validation: rule for value not exists in database


i´m using lumen 5.5 and want to prove that a given value is required, exists in table A and does NOT exist in table B.

while the first two rules can be found in the documentation I´m unable to find a solution for the third.

So this is what I currently use:

$rules = [
  'email' => 'required|exists:user,email'
];

Something like this is what I want:

$rules = [
  'email' => 'required|exists:user,email|not_exists:blocklist,email'
];

Someone know a simple validation rule for this?


Solution

  • Try this:

    $rules = [
      'email' => 'required|exists:users|unique:blocklist'
    ];
    

    Explanation:

    • With the exists rule we are making it sure that the provided email must exist in your users table under the column email.
    • With the unique rule we are making it sure that there must not exist a matching email in the blocklist table (under the email column).

    In both cases, I didn't specify a column name because the attribute name is the same than the matching column in the database. If you want to customize it just include it after a ,. e.g: required|exists:users,another_column