Dwight Watson's blog

Introducing nameable

April 22, 2020

I've been itching to give Laravel's new custom casting feature a go since it landed in 7.0. Cleaning up some of the full_name and initials attribute accessors I've got sprinkled through my apps looked like a good opportunity to port Basecamp's name_of_person library to Larave.

So I'm excited to introduce nameable for Laravel.

Nameable is a custom cast for your Eloquent models that gives you a number of handy accessors for your user's names. Getting started is really easy - just use the Nameable cast.

use Watson\Nameable\Nameable;

class User extends Model
    protected $casts = [
        'name' => Nameable::class,

Now the name attribute on your model will be an instance of Watson\Nameable\Name, giving you shorthand accessors to

$user = new User(['name' => 'Dwight Watson']);

$user->name->full        // Dwight Watson
$user->name->first       // Dwight
$user->name->last        // Watson
$user->name->familiar    // Dwight W.
$user->name->abbreviated // D. Watson
$user->name->sorted      // Watson, Dwight
$user->name->initials    // DCW

Have a look at the repo for more information and to get started.

A blog about Laravel & Rails by Dwight Watson;
developer of Roomies.com, myRent.co.nz, High School Notes & StudentVIP.com.au.

Follow me on Twitter, or GitHub.