namespace App\Models; use Illuminate\Database\Eloquent\Model; class User extends Model { /** * Get the user's first name. * * @param string $value * @return string */ public function getFirstNameAttribute($value) { return ucfirst($value); } /** * Set the user's password. * * @param string $value * @return void */ public function setPasswordAttribute($value) { $this->attributes['password'] = bcrypt($value); } /** * Get the user's full name. * This is an "appended" attribute that doesn't exist in the DB. * Add to $appends array in model. * * @return string */ public function getFullNameAttribute() { return "{$this->first_name} {$this->last_name}"; } protected $appends = ['full_name']; } // Usage: $user = App\Models\User::find(1); echo $user->first_name; // Automatically capitalized $user->password = 'new-secret-password'; // Automatically hashed echo $user->full_name; // Accesses the appended attribute $user->save();