
Recursive function example code PHP Laravel
The function which is called itself is a recursive function. Here we have an example code of recursive function which is used to create the username which is called itself till it does not get the unique username for a user while registration process.
Answers 1
-
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; }
0
Random Code Snippet Queries: Laravel
- Show old value while editing the form in Laravel
- Run artisan command to generate key in laravel
- How to restore deleted records in laravel
- Comment .env file in laravel
- How to check column value of a record is null or not in laravel
- Database transactions in laravel
- The use statement with non-compound name 'DB' has no effect
- How to update record after save method in Laravel
- Eager loading dynamically in laravel
- Update email with unique validation in laravel
- How to insert ckeditor data into database in Laravel?
- Class 'App\Providers\Auth' not found
- SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
- How to get random string in Laravel
- Shorter syntax for whereHas with call back function in laravel
- Insert values in pivot table dynamically in laravel
- Route group with URI prefix using middleware and route name prefixes
- How to check email is valid or not in Laravel
- SQLSTATE[23000]: Integrity constraint violation: 1022 Can't write; duplicate key in table
- Use withCount() to Calculate Child Relationship Records
- Call to a member function getRelationExistenceQuery() on array in Laravel
- Split an Eloquent Collection by half in Laravel
- Array to string conversion laravel Controller
- How to customize or Change validation error messages
- Laravel specific table Migration