Generate A Unique Username In Laravel When A User Registers - FreeCodeBlog

Last updated: Dec 10, 2021

Unique username in laravel! Yes you can generate a unique username in laravel when user will register her account. So do you want to make a username generate system in laravel? Yes. Good, Then you must need to read this unique name laravel tutorial to make unique username in laravel. In this tutorial, I will explain you how we can generate username in laravel when a user will registers. In this process you don't need to fill username input while user registration to make laravel username generator. So don't be late. Let's start reading to make unique username in laravel.

 

Generate A Unique Username In Laravel When A User Registers
Generate A Unique Username In Laravel When A User Registers

 

Unique Username In Laravel Ui.

In this tutorial, I will use laravel ui version 3 package for user authentication. If you haven't installed laravel ui 3 or don't know how to install laravel ui 3 then read install laravel ui package in any kind of laravel version tutorial. I have installed laravel ui 3.

 

Now go to register controller file from controller folder and add username in user create method.

 

Use below code if you want to generate unique username using user name's first word. It will generate unique username like “Masum” from “Masum Biswas” after registered.

 

'username' => explode(' ', $data['name'], 2)[0],

 

If you want to generate unique lower case user name in laravel like “masum” from user name's first word then use below line.

 

'username' => Str::lower(explode(' ', $data['name'], 2)[0]),

 

Unique Username In Laravel Jetstream.

You can also generate a unique username in laravel jetstream when a user register her account on your website. Read to install laravel jetstream user authentication first party package in laravel 8.

 

Now go to create new user controller file from actions fortify folder and add username in user create method.

 

'username' => explode(' ', $input['name'], 2)[0],

 

If you want to generate unique lower case user name in then use below line.

 

'username' => Str::lower(explode(' ', $input['name'], 2)[0]),

 

Unique Username In Laravel Breeze.

We can also generate a unique username in laravel breeze package. you can read install laravel breeze authentication first party package tutorial.

 

Go to registered controller file from controller folder and add username in user create method.

 

It will generate unique username like “Masum” from “Masum Biswas” after registered.

 

'username' => explode(' ', $request->name, 2)[0],

 

If you want to generate unique lower case user name in laravel like “masum” from user name's first word then use below line.

 

'username' => Str::lower(explode(' ', $request->name, 2)[0]),

 

Laravel Str For Unique Username.

Don't forgot to use str in register or create new user registered user controller. you will get laravel str related error in controller if you didn't use it top on the controller.

 

use Illuminate\Support\Str;

 

After successfully created and connected database connection you need to edit user table from database migration folder.

 

Add unique username string table in user migration file.

 

$table->string('username')->unique();

 

Now open user model and add username into fillable.

 

    protected $fillable = [
        'name',
        'username',
        'email',
        'password',
    ];

 

Now run migration command to migrate database.

 

php artisan migrate

 

I hope this tutorial help you to make a unique username in laravel.

 

Okay so this is for today. See you again next tutorials. Thanks

Post Tags
Post Writer
masum7323
Masum Biswas (@masum7323)

My name is Masum Biswas. I'm a full stack developer. I live in Bangladesh and I love to write tutorials and tools that can help to other artisan. I am a big fan of PHP, Javascript, JQuery, Laravel, MySQL, VueJS, Tailwind CSS and Bootstrap from the early stage.

Follow Me On
Related Posts
Laravel Ui Email Verification Complete Tutorial

Want to make laravel ui vue, react, bootstrap email verification using laravel ui version 3, 2, 1? Laravel ui has a feature for users to verify ema... Read More

Laravel Database Session Full Tutorial

Hello guys, In this tutorial I will explain about laravel database session. We can easily use database session driver for our laravel project. So l... Read More

Solve Ckeditor 5 Code Block Plugin Not Working In Laravel

Ckeditor 5 code block plugin not working in laravel? No. Do you have a programming laravel project and want to use code block ckeditor 5 plugin in... Read More

How To Make A Word Counter Online Tool In Laravel

Word counter in laravel? Yes. Do you want to make a word counter online tool using laravel? Yes. Want to publish a laravel word counter in laravel... Read More

How To Fix Laravel Session Error On Shared Hosting Cpanel

After uploading the laravel project to cpanel, you can get laravel session error in shared hosting cpanel. You need to fix laravel session error in... Read More