0
0
Laravelframework~10 mins

One-to-one (hasOne, belongsTo) 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 a one-to-one relationship in the User model using hasOne.

Laravel
public function profile() {
    return $this->[1](Profile::class);
}
Drag options to blanks, or click blank then click option'
AhasMany
BbelongsTo
ChasOne
DbelongsToMany
Attempts:
3 left
💡 Hint
Common Mistakes
Using belongsTo instead of hasOne here.
Confusing hasOne with hasMany.
2fill in blank
medium

Complete the code to define the inverse one-to-one relationship in the Profile model using belongsTo.

Laravel
public function user() {
    return $this->[1](User::class);
}
Drag options to blanks, or click blank then click option'
AbelongsTo
BhasOne
ChasMany
DbelongsToMany
Attempts:
3 left
💡 Hint
Common Mistakes
Using hasOne instead of belongsTo here.
Mixing up which model holds the foreign key.
3fill in blank
hard

Fix the error in this User model relationship method to correctly define a one-to-one relation.

Laravel
public function profile() {
    return $this->[1](Profile::class)->withTimestamps();
}
Drag options to blanks, or click blank then click option'
AhasOne
BbelongsTo
ChasMany
DbelongsToMany
Attempts:
3 left
💡 Hint
Common Mistakes
Using belongsTo instead of hasOne.
Trying to use hasMany for one-to-one.
4fill in blank
hard

Fill both blanks to define a one-to-one relationship with a custom foreign key and local key.

Laravel
public function passport() {
    return $this->[1](Passport::class, '[2]', 'id');
}
Drag options to blanks, or click blank then click option'
AhasOne
BbelongsTo
Cuser_id
Dpassport_id
Attempts:
3 left
💡 Hint
Common Mistakes
Using belongsTo instead of hasOne here.
Using the wrong foreign key name.
5fill in blank
hard

Fill all three blanks to define the inverse one-to-one relationship with custom keys.

Laravel
public function user() {
    return $this->[1](User::class, '[2]', '[3]');
}
Drag options to blanks, or click blank then click option'
AbelongsTo
Bpassport_id
Cid
Duser_id
Attempts:
3 left
💡 Hint
Common Mistakes
Using hasOne instead of belongsTo here.
Mixing up foreign key and owner key names.