Challenge - 5 Problems
Laravel Accessors & Mutators Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this accessor?
Given a Laravel model with this accessor, what will
Assuming
$user->full_name output?class User extends Model {
public function getFullNameAttribute() {
return strtoupper($this->first_name . ' ' . $this->last_name);
}
}Assuming
$user->first_name = 'Jane' and $user->last_name = 'Doe'.Laravel
class User extends Model { public function getFullNameAttribute() { return strtoupper($this->first_name . ' ' . $this->last_name); } } $user = new User(); $user->first_name = 'Jane'; $user->last_name = 'Doe'; echo $user->full_name;
Attempts:
2 left
💡 Hint
Remember that accessors modify the value when you access the attribute.
✗ Incorrect
The accessor method getFullNameAttribute returns the concatenated first and last name in uppercase. So accessing $user->full_name calls this method and returns 'JANE DOE'.
📝 Syntax
intermediate2:00remaining
Which mutator syntax is correct?
You want to create a mutator in a Laravel model to always store the email attribute in lowercase. Which of these method definitions is correct?
Attempts:
2 left
💡 Hint
Mutators use camelCase with set + Attribute name + Attribute.
✗ Incorrect
The correct mutator method name is setEmailAttribute and it must accept the $value parameter to modify before saving.
❓ state_output
advanced2:00remaining
What is the value of the attribute after setting it?
Consider this Laravel model mutator:
What will be stored in the database if you set
public function setUsernameAttribute($value) {
$this->attributes['username'] = trim(strtolower($value));
}What will be stored in the database if you set
$user->username = ' Alice123 ';?Laravel
public function setUsernameAttribute($value) {
$this->attributes['username'] = trim(strtolower($value));
}
$user = new User();
$user->username = ' Alice123 ';
echo $user->username;Attempts:
2 left
💡 Hint
Mutators change the value before saving it to the attributes array.
✗ Incorrect
The mutator trims whitespace and converts the username to lowercase before storing it. So ' Alice123 ' becomes 'alice123'.
🔧 Debug
advanced2:00remaining
Why does this accessor not work?
This Laravel model accessor is supposed to return the user's age, but it causes an error:
Assuming
public function getAgeAttribute() {
return $this->birthdate->diffInYears(now());
}Assuming
birthdate is a date string stored in the database, why does this cause an error?Attempts:
2 left
💡 Hint
Check the data type of birthdate when accessed in the model.
✗ Incorrect
Laravel does not automatically convert date strings to Carbon objects unless the attribute is cast as a date. Without casting, birthdate is a string and calling diffInYears on it causes an error.
🧠 Conceptual
expert2:00remaining
How do accessors and mutators affect JSON serialization?
In Laravel, when you convert a model to JSON using
toJson(), which of the following statements about accessors and mutators is true?Attempts:
2 left
💡 Hint
Think about when accessors and mutators run in the model lifecycle.
✗ Incorrect
Accessors modify attribute values when reading, so their computed values appear in JSON. Mutators modify values only when setting attributes, so they do not affect JSON output directly.