Download the PHP package luilliarcec/laravel-username-generator without Composer
On this page you can find all versions of the php package luilliarcec/laravel-username-generator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download luilliarcec/laravel-username-generator
More information about luilliarcec/laravel-username-generator
Files in luilliarcec/laravel-username-generator
Package laravel-username-generator
Short Description Laravel Username Generator is a package that allows the versatile generation of user names, has a simple integration with Laravel.
License MIT
Homepage https://github.com/luilliarcec/laravel-username-generator
Informations about the package laravel-username-generator
Laravel Username Generator
Laravel Username Generator is a package that allows the versatile generation of usernames, has a simple integration with Laravel.
You can generate from the name of the user, taking into account that you do not use more than two names and two surnames in total. It can also be generated from the user's email.
Installation
You can install the package via composer:
Version 5 was rewritten to be supported as a usable trait within your models.
Please follow this guide if you are going to update to the new version.
Upgrade
Upgrading to the new version is as easy as:
- Update package
- Delete the old configuration from your AppServiceProvider.
Usage
Add the Trait Luilliarcec\LaravelUsernameGenerator\Concerns\HasUsername
to your Eloquent models in the
use username.
Remember that you must have a field in your table where you can store the username
, preferably it is recommended
make this field unique
.
Within your model, you configure the username generator options.
By default, you must configure the field where the username will be stored and searched:
In addition to where the value will be extracted to generate the username:
Remember the value of name cannot be empty ''
, this will throw an exception, just like using a driver
incorrect for an incorrect value, for example using the Name
driver to generate usernames from an email.
With this, your model will now be configured to work with usernames.
Additional settings
If your model stores the first and last name separately, you can set the getLastName
function, so that it returns
the value of your model's last name.
By default, the trait uses the driver Luilliarcec\Laravel Username Generator\Drivers\Name
. This is modifiable
overriding the getUsernameDriver
method.
By default, usernames are converted to lowercase. But if you want, convert them to uppercase or apply
some logic or add a prefix or suffix, before the repeat search is processed, you can use the function
transformUsername
, this function receives the username as a parameter and returns a string.
Support for customs drivers
You can create a class that implement the
interface Luilliarcec\LaravelUsernameGenerator\Contracts\DriverContract
and inside that class you can write all the logic to generate your username, remember to implement the make method that
will be responsible for returning the username, for example:
¡Important!
Remember that like previous versions it is very important that you provide an Eloquent Model together with the column that stores the username. This is so that the package provides you with an alternate username if it is already in use.
Skipping this step will cause an exception UsernameGeneratorException
or that the generator does not work properly
Examples
Assume you have a user with the username larcec
When using the package to generate the username, it will search thanks to Eloquent, in the database and will buy if that username already exists, if it exists, a suffix will be added to the username.
The result would be as follows.
Laravel Username Generator uses a convention for the creation of user names, takes the first letter of the first name
,
takes the first last name
, and finally the first letter of the second last name
However, Laravel Username Generator is so versatile that it can receive only 1 name
, 1 name and 2 surnames
, and can
even use the auxiliary surname parameter to pass the two surnames separately
, in the following ways.
Keep these examples in mind, since passing a value of more or more than two names or two surnames without following the convention may cause an exception
Finally, you can use the email
driver, which will receive an email as the first and only parameter and take the user's
email and use it as a username.
Testing
Can use docker-compose to run
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
Credits
- Luis Andrés Arce C.
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
All versions of laravel-username-generator with dependencies
laravel/framework Version ^10.0|^11.0
spatie/laravel-package-tools Version ^1.14.0