0
0
Laravelframework~10 mins

Accessors and mutators in Laravel - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define an accessor method in a Laravel model.

Laravel
public function get[1]Attribute() {
    return strtoupper($this->attributes['name']);
}
Drag options to blanks, or click blank then click option'
AName
BNameAttribute
CgetName
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Using the attribute name in lowercase inside the method name.
Omitting 'Attribute' at the end of the method name.
2fill in blank
medium

Complete the code to define a mutator method in a Laravel model.

Laravel
public function set[1]Attribute($value) {
    $this->attributes['name'] = strtolower($value);
}
Drag options to blanks, or click blank then click option'
ANameAttribute
BsetName
Cname
DName
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase attribute name in the method name.
Omitting 'Attribute' suffix.
3fill in blank
hard

Fix the error in the accessor method name to follow Laravel conventions.

Laravel
public function get[1]() {
    return ucfirst($this->attributes['title']);
}
Drag options to blanks, or click blank then click option'
AgetTitleAttribute
Btitle
CTitleAttribute
DgetTitle
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting 'Attribute' suffix.
Using lowercase attribute name in method name.
4fill in blank
hard

Fill both blanks to create a mutator that stores the email attribute in lowercase.

Laravel
public function set[1]Attribute($value) {
    $this->attributes['email'] = [2]($value);
}
Drag options to blanks, or click blank then click option'
AEmail
Bstrtolower
Cstrtoupper
Demail
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase attribute name in method name.
Using strtoupper instead of strtolower.
5fill in blank
hard

Fill all three blanks to create an accessor that returns the user's full name in uppercase.

Laravel
public function get[1]Attribute() {
    return strtoupper($this->attributes['[2]'] . ' ' . $this->attributes['[3]']);
}
Drag options to blanks, or click blank then click option'
AFullName
Bfirst_name
Clast_name
Dfullname
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase or incorrect method name format.
Using wrong attribute keys inside the method.