Sample Recursive function in PHP using laravel eloquent
function generateSecureOtp($length = 6)
{
$characters = '23456789abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ';
$string = '';
for ($i = 0; $i < $length; $i++) {
$string .= $characters[mt_rand(0, strlen($characters) - 1)];
}
$exist = UserDetails::where('otp_value', $string)->first();
if($exist) {
return $this->generateSecureOtp(6);
}else {
return $string;
}
}
function generateSecureOtp($length = 6)
{
$characters = '23456789abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ';
$string = '';
for ($i = 0; $i < $length; $i++) {
$string .= $characters[mt_rand(0, strlen($characters) - 1)];
}
$exist = UserDetails::where('otp_value', $string)->first();
if($exist) {
return $this->generateSecureOtp(6);
}else {
return $string;
}
}