Generate unique username in Laravel
Created at 08-Mar-2022 ,
By samar
Generate unique username in Laravel
In this session, we are going to try to solve the "Generate unique username in Laravel" puzzle by using the computer language.
You can generate the unique username in Laravel from user's name. Here we will create the username and will also take care of that the record with same username is not already exists in users table.-
Generate unique username from user's name while creating user in Laravel
//routes\web.php Auth::routes(); //app\Models\User.php use Illuminate\Support\Str; protected $fillable = [ 'name', 'email', 'password', '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; } //app\Http\Controllers\Auth\RegisterController.php protected function create(array $data) { $userObject = New User; $userName = $userObject->generateUserName($data['name']); return User::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => Hash::make($data['password']), 'username'=>$userName, ]); } //Create a username column in the users table. ALTER TABLE `users` ADD `username` VARCHAR(200) NOT NULL AFTER `remember_token`;
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Laravel
- PhpMyAdmin - Error The mysqli extension is missing
- Call to undefined method Illuminate\Support\Facades\Request::all()
- File_put_contents(/var/www/html/w3code/storage/framework/sessions/CXwN3EXKxERD6jgy3rADcaAAbAx8FRKih2JK7UO9): Failed to open stream: Permission denied
- Route prefix with auth middleware in laravel
- Laravel URL validation not working
- Call to undefined method App\Models\User::follow()
- How to validate URL with https using regex in laravel
- How to get all route list
- How to get specific columns using Laravel eloquent methods
- Database transactions in laravel
- Multiple Level eager loading in Laravel
- Delete records with relationship in laravel
- How to add a key value pair to existing array in laravel
- Laravel onclick function not working
- Save or update pivot table data with additional column in Laravel
- Argument 1 passed to App\Http\Controllers\Auth\LoginController::authenticated() must be an instance of App\Http\Controllers\Auth\Request
- Session Doesn't Work on Redirect
- Docker important commands to run laravel application with docker
- How to use more than one query scope in Laravel
- How to insert value to additional columns in pivot table in laravel
- Ignore Records where a field has NULL value in Laravel
- Get current URL on visit URL in Laravel
- Argument 1 passed to Symfony\Component\HttpFoundation\Response::setContent() must be of the type string or null, object given
- Laravel delete all rows older than 30 days
- How to get last month records in Laravel