The table structure:
users: id - email - active
I want check duplicated email only when active is 0.
For all emails,I do this:
'email' => 'required|unique:users|email',
Is it possible to define a condition in the validation?
What about using validator closure ?
public function store(Request $request)
{
$this->validate($request, [
'email' => [
'required',
'email',
function ($attribute, $value, $fail) {
if (Users::whereEmail($value)->whereActive(0)->count() > 0) {
$fail($attribute.' is already used.');
}
},
]);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With