taylornetwork/laravel-username-generator

How do I use this package if my User model doesn't have name column ?

vbasky opened this issue · 5 comments

I get an error when I try to add the Trait to the User Model with

use FindSimilarUsernames, GeneratesUsernames;

Return value of App\Models\User::getField() must be of the type string, null returned {"exception":"[object] (TypeError(code: 0): Return value of App\\Models\\User::getField() must be of the type string, null returned at /Users/vikram/Sites/.../vendor/taylornetwork/laravel-username-generator/src/GeneratesUsernames.php:52)
[stacktrace]

Are you using a different column or field to pass to the generator or are you trying to get a random unique username?

Hi @vbasky

I've just released a new version, 2.3.2, that will likely solve your problem. I'm going to leave this open for now, please let me know if it worked or if you're having a different issue.

Thanks

I don't have a name field. I only have firstname and a lastname field and want the username to be generated of the firstname lastname combination

Also another thing noted is that config generates a App/User as the model. Laravel 8 defaults to App/Models/User not a big deal but just wanted to mention

Ah okay gotcha. In that case you just need to override the getField() method in your User model.

Add this to your User model

public function getField()
{
    return $this->first_name . ' ' . $this->last_name;
}

Yeah I did notice that, I'm going to publish a 2.4 version shortly that addresses that, thanks!

Let me know if that worked for you

Yes that worked verified even before 2.3.2 bug fix. And the big fix works too without the getField I don't an error I get a random username generated. That's more like what I wanted.

It works and I'll close this issue.