Download the PHP package taylornetwork/name-formatter without Composer
On this page you can find all versions of the php package taylornetwork/name-formatter. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download taylornetwork/name-formatter
More information about taylornetwork/name-formatter
Files in taylornetwork/name-formatter
Package name-formatter
Short Description A name formatter for Laravel
License MIT
Informations about the package name-formatter
Name Formatter for Laravel
A customizable name formatter class for Laravel.
Install
Via Composer
Publish Config
This will add nameformatter.php to your config directory
Usage
Create a new instance of the class by passing it an instance of Illuminate\Eloquent\Model and call format()
Let's say we have a Customer model at App\Customer
We want to add a fullName attribute to our customer.
We can create a getAttribute method Laravel will use to create the attribute for us.
See Laravel Eloquent Documentation for more information.
Get the customer's full name
Returns
By default the Formatter class will concatenate the first_name attribute, a space and the last_name attribute.
Trait
This package includes a trait you can add to your model that will add a fullName attribute.
You can then access the full name using the default configuration by:
Override Formatter Config
You can override the formatter config when using the trait by overriding the formatterConfig method in your model
Available Methods
format ()
Returns formatted name.
map (string | array $field, string | null $modelField)
By default the Formatter class will look for a first_name and last_name attribute on the model it was passed. If the model you are passing it has different attribute names fot first and last names you can either pass an associative array to the map function or two strings.
For a model with a first name attribute named fName and a last name attribute named lName
style (string $style)
By default the Formatter class will format names as $first_name . ' ' . $last_name
You can override the style with the style function. style accepts a string with the formatting you would like Formatter to use
| Key | Description | Example: 'John Doe' |
|---|---|---|
'F' |
The full first name | 'John' |
'L' |
The full last name | 'Doe' |
'f' |
The first initial | 'J' |
'l' |
The last initial | 'D' |
Any other characters in the string will appear in the formatted name
Examples
To get the first initial and last name
To get the last initial, the first name
To get the first and last initials
You can even add other characters to the style string
Every key in the string is replaced, so you could do something like this. (Though I don't know why you would)
Config
Once you run php artisan vendor:publish the config file nameformatter.php will be in your config directory. There you can set the defaults you want in terms of format style, field map, etc.
Credits
- Author: Sam Taylor
License
The MIT License (MIT). Please see License File for more information.