Skip to content
Sam Taylor edited this page Aug 2, 2022 · 5 revisions

Details of all the config options, select one from the list to view more details.

Unique

Key: 'unique'

Type: Boolean

Default Value:

false

Description:

Should the username be unique to the Model class?

Minimum Length

Key: 'min_length'

Type: Integer

Default Value:

0

Description:

What is the minimum length a username needs to be?

Notes:

Set to 0 to not enforce.

Throw Exception on Too Short

Key: 'throw_exception_on_too_short'

Type: Boolean

Default Value:

false

Description:

Throw a UsernameTooShortException if username is shorter than the 'min_length'?

Notes:

Set to false to automatically pad the username to make it longer.

Maximum Length

Key: 'max_length'

Type: Integer

Default Value:

0

Description:

What is the maximum length a username can be?

Notes:

Set to 0 to not enforce.

Throw Exception on Too Long

Key: 'throw_exception_on_too_long'

Type: Boolean

Default Value:

false

Description:

Throw a UsernameTooLongException if username is longer than the 'max_length'?

Notes:

Set to false to automatically reduce the length of the username.

Character Case

Key: 'case'

Type: String

Default Value:

'lower'

Description:

Function to transform the string's casing.

Suggested:

  • 'lower'
  • 'upper'
  • 'title'

Notes:

  • Uses the Illuminate\Support\Str class behind the scenes. You can use any string function that accepts 1 parameter and returns the transformed text.
  • Ignore using 'mixed' (or any other value).

Separator

Key: 'separator'

Type: String*

Default Value:

''

Description:

Character to replace all spaces between words with.

Suggested:

  • ''
  • '_'
  • '-'

Notes:

* Should probably be a single character, but can be multiple.

Model

Key: 'model'

Type: String

Default Value:

\App\Models\User::class

Description:

Model to check username's uniqueness against.

Notes:

Username Column

Key: 'column'

Type: String

Default Value:

'username'

Description:

Username column to use for unique check on model.

Validate Characters

Key: 'validate_characters'

Type: Boolean

Default Value:

true

Description:

Should we validate characters with RegEx? Will use the 'allowed_characters' as the expression.

Allowed Characters

Key: 'allowed_characters'

Type: String*

Default Value:

'a-zA-Z\s '

Description:

Characters (or character sets) that are allowed in the final username. All characters not in this expression are automatically replaced by ''. If you are using different character sets ('\p{Greek}' for example) that you want in the final username, be sure to change 'convert_to_ascii' to false.

Suggested:

  • 'a-zA-Z\s '
  • '\p{Latin}\s '

Notes:

* A regular expression, without the exterior quotes.

Character Encoding

Key: 'encoiding'

Type: String

Default Value:

'UTF-8'

Description:

Encoding to use with the mb_* PHP string helpers. 'UTF-8' is likely fine for most cases.

Convert to ASCII

Key: 'convert_to_ascii'

Type: Boolean

Default Value:

true

Description:

Instead of working with other character sets, convert input to ASCII immediately.

Notes:

Set to false if you use any other character sets set out in 'allowed_characters'.

Language

Key: 'language'

Type: String

Default Value:

'en'

Description:

Used when converting the input to ASCII.

Generate on Entered Usernames

Key: 'generate_entered_username'

Type: Boolean

Default Value:

true

Description:

If a user supplies their own username, should it be passed through the generator to make sure all usernames are the same?

Notes:

This only applies if your 'model' is also using the GeneratesUsernames trait.

Prefer SQL REGEXP Function

Key: 'prefer_regexp'

Type: Boolean

Default Value:

false

Description:

Prefer using the SQL REGEXP function for finding similar usernames?

If set to false the SQL LIKE function will be used to find similar usernames.

If set to true the SQL REGEXP function will attempt to find similar usernames, if that fails, LIKE will be used.

Field Map

Key: 'field_map'

Type: Array

Default Value:

[]

Description:

In some cases, your 'model' field structure won't match that of the driver you're using. In that case you may need to use a field map. Here you can define the driver's field key to what it is on your model.

For example if your model has a fullName field, since the NameDriver looks for a name field:

[
    'name' => 'fullName',
]

Increment to Make Unique (Max Attempts)

Key: 'increment_max_attempts'

Type: Integer

Default Value:

100

Description:

If the username fails to become unique after using other methods, the last number will be incremented until a unique value is given, this option limits the amount of attempts it can make.

This is used in a loop so if you put the value too high the execution time may suffer, but this is a last resort attempt to make the username unique.

Registered Drivers

Key: 'drivers'

Type: Array

Default Value:

[
    'name'  => \TaylorNetwork\UsernameGenerator\Drivers\NameDriver::class,
    'email' => \TaylorNetwork\UsernameGenerator\Drivers\EmailDriver::class,
]

Description:

All the regiestered drivers and their keys.

The first item in this array is the default driver used.

Anything in this array can be used by the setDriver('driverKey') method or the ::usingDriverKey() static method.

Alternate Dictionary

Key: 'dictionary'

Type: Array

Default Value:

[
    'adjectives' => [],
    'nouns'      => [],
]

Description:

If you don't want to use the default word lists, add your own 'nouns' and 'adjectives' word lists here.

Default word lists from alenoir/username-generator