Laravel recursive function in controller

Updated at 06-Oct-2021, By samar

Laravel recursive function in controller

In this session, we’ll try our hand at solving the "Laravel recursive function in controller" puzzle by using the computer language.

Recursive function calls itself directly or indirectly. Here we have a recursive function in controller which call itself until the condition becomes false is called by another method in controller
  • Recursive function in Laravel to generate unique username

    public function generateUserName($name){
        $username = Str::lower(Str::slug($name));
        if(User::where('username', '=', $username)->exists()){
            $uniqueUserName = $username.'-'.Str::lower(Str::random(4));
            $username = $this->generateUserName($uniqueUserName);
        }
        return $username;
    }
    

Back to code snippet queries related laravel