Eloquent Accessors & Mutators OR Custom Attributes

The accessor and mutator both are the public functions of Laravel model for getting and setting model's attributes.

An accessor will automatically be called by Eloquent when attempting to retrieve the value of the first_name attribute:

$fullName = $user->full_name;

It's for customizing a model's attributes or adding fake attributes.

On the other hand, the mutator is for setting a real attribute of a model.

Mutator will be automatically called when we attempt to set the value of an attribute. 

  • 249
  • 308
  • By Bablu Ahmed
  • Posted 1 year ago