0
0
Laravelframework~20 mins

Accessors and mutators in Laravel - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Laravel Accessors & Mutators Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this accessor?
Given a Laravel model with this accessor, what will $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;
Ajane doe
BJane Doe
CJANE DOE
DError: Undefined property full_name
Attempts:
2 left
💡 Hint
Remember that accessors modify the value when you access the attribute.
📝 Syntax
intermediate
2: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?
A
public function setEmail($value) {
    $this->attributes['email'] = strtolower($value);
}
B
public function setEmailAttribute($value) {
    $this->attributes['email'] = strtolower($value);
}
C
public function set_email_attribute($value) {
    $this->attributes['email'] = strtolower($value);
}
D
public function setEmailAttribute() {
    $this->attributes['email'] = strtolower($value);
}
Attempts:
2 left
💡 Hint
Mutators use camelCase with set + Attribute name + Attribute.
state_output
advanced
2:00remaining
What is the value of the attribute after setting it?
Consider this Laravel model mutator:

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;
AError: Undefined property username
B Alice123
CALICE123
Dalice123
Attempts:
2 left
💡 Hint
Mutators change the value before saving it to the attributes array.
🔧 Debug
advanced
2:00remaining
Why does this accessor not work?
This Laravel model accessor is supposed to return the user's age, but it causes an error:

public function getAgeAttribute() {
    return $this->birthdate->diffInYears(now());
}

Assuming birthdate is a date string stored in the database, why does this cause an error?
Abirthdate is a string, not a Carbon date object, so diffInYears method is undefined
BThe birthdate attribute is private and cannot be accessed
CThe now() function is not imported and causes a fatal error
DThe accessor method name is incorrect; it should be get_age_attribute
Attempts:
2 left
💡 Hint
Check the data type of birthdate when accessed in the model.
🧠 Conceptual
expert
2: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?
AAccessors are applied and their values appear in the JSON, but mutators do not affect JSON output
BMutators modify the JSON output values, but accessors do not
CNeither accessors nor mutators affect the JSON output; only raw attributes are shown
DBoth accessors and mutators modify the JSON output values
Attempts:
2 left
💡 Hint
Think about when accessors and mutators run in the model lifecycle.