0
0
Laravelframework~10 mins

Accessors and mutators in Laravel - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Accessors and mutators
Model instance created
Set attribute value
Mutator modifies value before saving
Value saved in database
Get attribute value
Accessor modifies value before returning
Return modified value to user
This flow shows how Laravel modifies model attributes when saving and retrieving using mutators and accessors.
Execution Sample
Laravel
class User extends Model {
  public function setNameAttribute($value) {
    $this->attributes['name'] = strtolower($value);
  }
  public function getNameAttribute($value) {
    return ucfirst($value);
  }
}
$user = new User();
$user->name = 'ALICE';
echo $user->name;
This code sets a user's name with a mutator that lowercases it before saving, and an accessor that capitalizes it when retrieved.
Execution Table
StepActionInput ValueMutator OutputAccessor OutputResult
1Create User instance---User object created
2Set name attributeALICEalice-Name stored as 'alice' internally
3Get name attribute--AliceAccessor returns 'Alice'
4Echo name--AliceOutput: Alice
5End---Execution complete
💡 All steps executed; final output is 'Alice' after accessor modifies stored value.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
$user->attributes['name']nullalicealicealice
$user->name (accessor)nullN/AAliceAlice
Key Moments - 2 Insights
Why does the stored name become lowercase but the output is capitalized?
The mutator changes the input to lowercase before saving (see Step 2), and the accessor capitalizes the stored value when retrieving it (see Step 3).
What happens if you access the attribute directly without the accessor?
Accessing the raw attribute (like $user->attributes['name']) returns the lowercase value stored by the mutator, not the capitalized version.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of $user->attributes['name'] after Step 2?
Aalice
BALICE
CAlice
Dnull
💡 Hint
Check the 'Mutator Output' column in Step 2 of the execution table.
At which step does the accessor modify the value before returning it?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Accessor Output' column in the execution table.
If the mutator was removed, what would be the output at Step 4?
AAlice
BALICE
Calice
Dnull
💡 Hint
Without the mutator, the original input is stored; see Step 2 mutator output for comparison.
Concept Snapshot
Laravel Accessors and Mutators:
- Mutators modify attribute values before saving to database.
- Accessors modify attribute values when retrieving.
- Define mutator: set{Attribute}Attribute($value).
- Define accessor: get{Attribute}Attribute($value).
- Helps keep data consistent and formatted.
Full Transcript
In Laravel, accessors and mutators let you change how model attributes are saved and retrieved. When you set an attribute, a mutator can change the value before it is saved in the database. When you get an attribute, an accessor can change the value before it is returned. For example, a mutator can lowercase a name before saving, and an accessor can capitalize it when you get it. This keeps data consistent and formatted nicely. The execution table shows each step: creating the user, setting the name (mutator lowercases), getting the name (accessor capitalizes), and outputting the final value. The variable tracker shows how the stored and accessed values differ. Understanding these steps helps avoid confusion about why stored and displayed values differ.