|
| 1 | +--- |
| 2 | +title: Name |
| 3 | +sort: 3 |
| 4 | +--- |
| 5 | + |
| 6 | +## Introduction |
| 7 | + |
| 8 | +The name cast provides a cast/formatter for presenting your user's names. It can get a user's first, last or full name, |
| 9 | +their initials, and common abbreviations. This cast will also allow you to store a user's name in a single column and |
| 10 | +fetch what you need. |
| 11 | + |
| 12 | +This cast is based on the [nameable](https://github.com/dwightwatson/nameable) cast by [@dwightwatson](https://github.com/dwightwatson). |
| 13 | + |
| 14 | +## Usage |
| 15 | + |
| 16 | +Add either the `NameCast` or `Name` class as a cast on your model's name field. Using either class will give the same result. |
| 17 | + |
| 18 | +```php |
| 19 | +<?php |
| 20 | + |
| 21 | +namespace App\Models; |
| 22 | + |
| 23 | +use Illuminate\Database\Eloquent\Model; |
| 24 | +use Rawilk\LaravelCasters\Casts\NameCast; |
| 25 | +use Rawilk\LaravelCasters\Support\Name; |
| 26 | + |
| 27 | +class User extends Model |
| 28 | +{ |
| 29 | + protected $casts = [ |
| 30 | + 'name' => Name::class, |
| 31 | + |
| 32 | + // Or |
| 33 | + // 'name' => NameCast::class, |
| 34 | + ]; |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +## Manipulations |
| 39 | + |
| 40 | +There are several manipulations you can perform on a `Name` instance. Below is a demonstration of each of them |
| 41 | +using the `User` model defined above: |
| 42 | + |
| 43 | +```php |
| 44 | +$user = new User(['name' => 'John Smith']); |
| 45 | + |
| 46 | +echo $user->name->full; // 'John Smith' |
| 47 | +echo $user->name->first; // 'John' |
| 48 | +echo $user->name->last; // 'Smith' |
| 49 | +echo $user->name->familiar; // 'John S.' |
| 50 | +echo $user->name->abbreviated; // 'J. Smith' |
| 51 | +echo $user->name->sorted; // 'Smith, John' |
| 52 | +echo $user->name->initials; // 'JS' |
| 53 | +``` |
| 54 | + |
| 55 | +There are also possessive variants you can use which will even work with names that end in `s`: |
| 56 | + |
| 57 | +```php |
| 58 | +$user = new User(['name' => 'John Doe']); |
| 59 | + |
| 60 | +echo $user->name->full_possessive; // John Doe's |
| 61 | +echo $user->name->first_possessive; // John's |
| 62 | +echo $user->name->last_possessive; // Doe's |
| 63 | +echo $user->name->abbreviated_possessive; // J. Doe's |
| 64 | +echo $user->name->sorted_possessive; // Doe, John's |
| 65 | +echo $user->name->initials_possessive; // JD's |
| 66 | + |
| 67 | +$user = new User(['name' => 'Angus Young']); |
| 68 | + |
| 69 | +$user->name->full_possessive; // Angus Young's |
| 70 | +$user->name->first_possessive; // Angus' |
| 71 | +``` |
| 72 | + |
| 73 | +If a user doesn't provide a full name (for example, just a first name) the attributes will just omit the last name. |
| 74 | + |
| 75 | +## Casting from multiple fields |
| 76 | + |
| 77 | +If you prefer to separate your user's first and last names in the database, this cast will still work as well. |
| 78 | +You can create a "name" field at runtime on your model from your model's first and last name attributes. |
| 79 | + |
| 80 | +If you have a `first_name` and `last_name` attribute on your model, you can cast it like this: |
| 81 | + |
| 82 | +```php |
| 83 | +<?php |
| 84 | + |
| 85 | +namespace App\Models; |
| 86 | + |
| 87 | +use Illuminate\Database\Eloquent\Model; |
| 88 | +use Rawilk\LaravelCasters\Support\Name; |
| 89 | + |
| 90 | +class User extends Model |
| 91 | +{ |
| 92 | + // $fillable not needed, just here to show the user has those fields... |
| 93 | + protected $fillable = ['first_name', 'last_name']; |
| 94 | + |
| 95 | + protected $casts = [ |
| 96 | + 'name' => Name::class, |
| 97 | + ]; |
| 98 | +} |
| 99 | +``` |
| 100 | + |
| 101 | +Now when you create a new User model with a first and last name, it will be able to grab them and instantiate a new `Name` instance from them. |
| 102 | + |
| 103 | +```php |
| 104 | +$user = new User(['first_name' => 'John', 'last_name' => 'Smith']); |
| 105 | + |
| 106 | +echo $user->name->full; // 'John Smith' |
| 107 | +echo $user->name->initials; // 'JS' |
| 108 | +``` |
| 109 | + |
| 110 | +### Using custom first and last name attribute names |
| 111 | + |
| 112 | +If you have your first or last name attributes named something different in the database, you can specify the column names in the |
| 113 | +cast definition: |
| 114 | + |
| 115 | +```php |
| 116 | +<?php |
| 117 | + |
| 118 | +namespace App\Models; |
| 119 | + |
| 120 | +use Illuminate\Database\Eloquent\Model; |
| 121 | +use Rawilk\LaravelCasters\Support\Name; |
| 122 | + |
| 123 | +class User extends Model |
| 124 | +{ |
| 125 | + protected $casts = [ |
| 126 | + 'name' => Name::class . ':given_name,family_name', |
| 127 | + ]; |
| 128 | +} |
| 129 | +``` |
| 130 | + |
| 131 | +Now the name cast will use the `given_name` attribute as the user's "first_name", and |
| 132 | +the `family_name` attribute as the user's "last_name" when casting the name attribute. |
| 133 | + |
| 134 | +```php |
| 135 | +$user = new User(['given_name' => 'John', 'family_name' => 'Smith']); |
| 136 | + |
| 137 | +echo $user->name->full; // 'John Smith' |
| 138 | +``` |
| 139 | + |
| 140 | +## Using Without a Model |
| 141 | + |
| 142 | +You can alternatively use the `Name` class without it being a cast on a model. Just provide a name to it directly: |
| 143 | + |
| 144 | +```php |
| 145 | +<?php |
| 146 | + |
| 147 | +namespace App; |
| 148 | + |
| 149 | +use Rawilk\LaravelCasters\Support\Name; |
| 150 | + |
| 151 | +$name = new Name('Randall', 'James Wilk'); |
| 152 | + |
| 153 | +// or |
| 154 | +$name = Name::from('Randall James Wilk'); |
| 155 | + |
| 156 | +echo $name->full; // Randall James Wilk |
| 157 | +echo $name->initials; // RJW |
| 158 | +``` |
0 commit comments