Event::where('guest_code', $code)->exists() always return false. Try to think.
Apr 4, 2023
15
Level 63
Recursive function to generate a unique code
Hello,
I have written this code in a service.
class UniqueGuestCodeGeneration
{
public function generate(int $codeLength)
{
$characters = '123456789ABCDEFGHJKMNPQRSTUVWXYZ';
$charactersNumber = strlen($characters);
$code = '';
while (strlen($code) < $codeLength) {
$position = rand(0, $charactersNumber - 1);
$character = $characters[$position];
$code = $code.$character;
}
if (Event::where('guest_code', $code)->exists()) {
$this->generate();
}
return $code;
}
}
I didn't matter that I use the recursive function $this->generate(); without passing the $codeLength variable.
And it works !
Why ?
Shoudn't it generate an error ?
Can you help me to understand ?
Thanks ;).
V
Level 75
1 like
Please or to participate in this conversation.