Accessors and mutators let you change how data is read from or saved to your database in Laravel models. They help keep your code clean and consistent.
Accessors and mutators in Laravel
class User extends Model { // Accessor public function getAttributeNameAttribute($value) { return ucfirst($value); } // Mutator public function setAttributeNameAttribute($value) { $this->attributes['attribute_name'] = strtolower($value); } }
Accessor methods start with get, end with Attribute, and receive the original value.
Mutator methods start with set, end with Attribute, and receive the new value to store.
full_name attribute by joining first and last names.public function getFullNameAttribute() {
return $this->first_name . ' ' . $this->last_name;
}public function setPasswordAttribute($value) {
$this->attributes['password'] = bcrypt($value);
}created_at date when accessed.public function getCreatedAtAttribute($value) {
return date('d-m-Y', strtotime($value));
}This example shows a User model with an accessor and mutator for the name attribute. When setting the name, it saves in lowercase. When getting the name, it returns with the first letter capitalized.
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; class User extends Model { // Accessor to capitalize the name when accessed public function getNameAttribute($value) { return ucfirst($value); } // Mutator to store the name in lowercase public function setNameAttribute($value) { $this->attributes['name'] = strtolower($value); } } // Usage example $user = new User(); $user->name = 'ALICE'; // Mutator converts to 'alice' echo $user->name; // Accessor converts to 'Alice'
Accessors and mutators only work on model attributes, not on regular methods.
Make sure method names exactly follow the getXAttribute and setXAttribute pattern, where X is the attribute name in StudlyCase.
Use accessors to format or modify data when reading, and mutators to change data before saving.
Accessors change how you get data from a model attribute.
Mutators change how you set data on a model attribute.
They help keep your data consistent and your code clean.
Practice
Solution
Step 1: Understand what an accessor does
An accessor changes how you get or read a model attribute's value.Step 2: Differentiate from mutator
A mutator changes the value when setting or saving, not getting.Final Answer:
To modify the value when retrieving a model attribute -> Option DQuick Check:
Accessor = get attribute value [OK]
- Confusing accessor with mutator
- Thinking accessor validates data
- Mixing up relationships with accessors
name attribute in a Laravel model?Solution
Step 1: Recall accessor naming convention
Accessors use the format get{Attribute}Attribute with the attribute name capitalized.Step 2: Check method signature and return
The method receives the raw value and returns the modified value.Final Answer:
public function getNameAttribute($value) { return strtoupper($value); } -> Option CQuick Check:
Accessor method = getNameAttribute [OK]
- Using set prefix for accessor
- Wrong method naming like get_name
- Not returning a value
$user->full_name output?class User extends Model {
public function getFullNameAttribute() {
return $this->first_name . ' ' . $this->last_name;
}
}
$user = new User();
$user->first_name = 'Jane';
$user->last_name = 'Doe';
echo $user->full_name;Solution
Step 1: Understand the accessor method
The method getFullNameAttribute concatenates first_name and last_name with a space.Step 2: Check assigned values and output
first_name is 'Jane', last_name is 'Doe', so full_name returns 'Jane Doe'.Final Answer:
Jane Doe -> Option BQuick Check:
Accessor combines names with space [OK]
- Forgetting space between names
- Expecting property to exist without accessor
- Confusing method name with property
email attribute:public function setEmailAttribute($email) {
$this->email = strtolower($email);
}Solution
Step 1: Check mutator assignment syntax
Mutators must assign to $this->attributes['attribute'], not directly to $this->attribute.Step 2: Confirm method name and behavior
Method name setEmailAttribute is correct; no return needed.Final Answer:
Should assign to $this->attributes['email'], not $this->email -> Option AQuick Check:
Mutator assigns to attributes array [OK]
- Assigning directly to $this->property
- Using accessor naming for mutator
- Expecting return value in mutator
public function setPhoneNumberAttribute($value) {
// Remove spaces and dashes
$clean = str_replace([' ', '-'], '', $value);
// Save cleaned number
???
}Solution
Step 1: Understand mutator assignment
Mutators must assign cleaned value to $this->attributes['phone_number'] to save properly.Step 2: Check other options
Assigning to $this->phone_number is incorrect; returning value or calling setAttribute with original value won't clean data.Final Answer:
$this->attributes['phone_number'] = $clean; -> Option AQuick Check:
Mutator saves cleaned value to attributes array [OK]
- Assigning directly to property instead of attributes
- Returning value instead of assigning
- Using original value instead of cleaned
Ready to prove it?
Pass the test and this pattern moves unknown
