We participate in the Amazon Service LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com

Laravel Eloquent Attributes

When dealing with an Eloquent model, you can add your own attributes. These can be useful for computed values.

For example:

class Person extends Model
{
   // By default, all the database fields will be available.
   // Let's assume for this example class that we have
   // first_name, last_name, age
   // but we also want a property called full_name, that doesn't 
   // exist in the database. 

   public function getFullNameAttribute()
   {
      return $this->first_name . ' ' . $this->last_name;
   }

}

// To access that property in a controller, you use the camelCase version of it:
// (assuming you already have an object instance called $aPerson)

$fullName = $aPerson->fullName;

// To access the property directly from the class instance in a view using blade, 
//(assuming that you passed the instance to the view), you use the snake_case version of it

{{ $aPerson->full_name }}

Just remember that when creating an attribute, you start with get and end with Attribute, and the name goes in the middle. The whole thing is camelCase. This is Laravel’s convention.