hakanylmz Bu şekilde kullanabilirsiniz ama yazdığınız servis pek bir anlam ifade etmiyor. Aslında yaptığınız işlem şu:
static::creating(function ($user) {
$user->private_key = retry(5, function () {
$key = Str::random(20);
throw_if(User::where('private_key', $key)->doesntExist());
return $key;
});
});
Servis olacaksa:
class PrivateKeyService
{
public function check($key): bool
{
return User::where('private_key', $key)
->exists();
}
public function generate(): string
{
$key = Str::random(20);
while($this->check($key)) {
$key = Str::random(20);
}
return $key;
}
}
static::creating(function ($user) {
$user->private_key = resolve(PrivateKeyService::class)->generate();
});