It is providing us with a full name attribute that does not even exists. It combines first name, middle name and last name, removes the empty ones and joins it with space. So when required, you can simply call $user->full_name. Similarly, we can also define setAgeAttribute(); or something like that in an eloquent model. For example, setAgeAttribute that calculates age based upon birthday.
A getter is a method that gets the value of a specific property. A setter is a method that sets the value of a specific property. You can define getters and setters on any predefined core object or user-defined object that supports the addition of new properties. The syntax for defining getters and setters uses the object literal syntax.
Take a look at this example.
Notice that, a ‘code’ is an object that contains a repo of letters with mutators.
To retrieve all letters, we can simply do
Or to set letters, we can simply do
Not just that,
We can have additional getter methods that just return some specific values like:
Everything is awesome.