saqueib's avatar

How I can generate unique username from user first name and last name, like laracasts does

I am developing an app which asks first and last name from user and email to create there profile.

I want to have the username automatically made using first/last name, but its must be unique. How I can do it.

0 likes
12 replies
absiddiqueLive's avatar

@saqueib , I think/let Your Model name User and column name username

static public function getUsername($firstName, $lastName) {
        $username = Str::slug($firastName . "-" . $lastName);
        $userRows  = User::whereRaw("username REGEXP '^{$username}(-[0-9]*)?$'")->get();
        $countUser = count($userRows) + 1;

        return ($countUser > 1) ? "{$username}-{$countUser}" : $username;
    }
3 likes
saqueib's avatar

Thanks @absiddiqueLive, one question what about if {$username}-{$countUser} was also present in db, since its only checking once and appending a number at the end of username?

jarichypma's avatar

You could also consider a slightly different approach. Why not use the email as unique identifier for a user? That way you have an easy (and verifyable way) to determine if a user is unique. In that case you don't have to make changes to the first and lastname either.

1 like
absiddiqueLive's avatar

@saqueib , Yes . If a user exist with username touch then it will return touch-2 , if you want to remove dash you can !

{$username}{$countUser} //like this
saqueib's avatar

@jarichypma I am storing the email and its unique but i need a profile page for user like

example.com/@john

The name should be the username, like its in Laracasts

laracasts.com/@jarichypma

as you said if i explode('@', 'email@example.com') and use first part of it, problem will be same, How can i lookup in db and get a unique name.

If @JeffreyWay can help that will be fatasitic as he has done it in Laracasts

jarichypma's avatar

I see.

Well in that case, does the profile-page have to public? If not, then:

example.com\profile

should be sufficient.

If the profilepage should be publicly accessable then why not explicitly ask the user for a username. That gives the user the freedom to take whatever nickname he wants, and gives you the advantage of easy validating a name against a column in your database.

If you want to make it even prettier, have an ajax-request validate the username realtime against the database. Using vue.js this isn't that hard to write.

Does this answer your question?

1 like
saqueib's avatar

@sid405 Yes I do have an username field in users table, but i am not asking user to enter username in signup form, I want to generate unique username automatically based on first/last name , later user can change it if they wants by visiting account setting

zachleigh's avatar

Why not just let them choose? If you have 10 people named 'Bob Smith' sign up and you force a username on them, most will probably forget it.

1 like
sid405's avatar
sid405
Best Answer
Level 27

@saqueib Although i really don't see the point of letting them choose, you can generate a unique id concatenating 'Bob' with the hashid https://github.com/vinkla/hashids of the user->id.

But like @zachleigh said you'll end up with 10 users whose username will start with bob and then 10 digits of gibberish.

Really don't see a point in this, unless there's a part of the application logic you're not mentioning.

1 like
decebal2dac's avatar

Same problem here, I really need to generate an username as my users get to choose a username after that

Please or to participate in this conversation.