anonaddy/app/Rules/UniqueRecipient.php
2023-04-26 16:53:39 +01:00

37 lines
910 B
PHP

<?php
namespace App\Rules;
use App\Models\Recipient;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
class UniqueRecipient implements ValidationRule
{
/**
* Indicates whether the rule should be implicit.
*
* @var bool
*/
public $implicit = true;
/**
* Run the validation rule.
*/
public function validate(string $attribute, mixed $value, Closure $fail): void
{
$items = Recipient::where('user_id', user()->id)
->orWhere('email_verified_at', '!=', null)
->select('email')
->get()
->filter(function ($recipient) use ($value) {
if (($recipient->email) == strtolower($value)) {
return $recipient;
}
});
if (count($items) !== 0) {
$fail('A recipient with that email already exists.');
}
}
}