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

34 lines
785 B
PHP

<?php
namespace App\Rules;
use App\Models\DeletedUsername;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
class NotDeletedUsername 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
{
$deletedUsernames = DeletedUsername::select('username')
->get()
->map(function ($item) {
return $item->username;
})
->toArray();
if (in_array(strtolower($value), $deletedUsernames)) {
$fail('The :attribute has already been taken.');
}
}
}