0
0
Laravelframework~10 mins

Has-many-through 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 has-many-through relationship in a Laravel model.

Laravel
public function posts() {
    return $this->[1](Post::class, Country::class);
}
Drag options to blanks, or click blank then click option'
AhasManyThrough
BbelongsToMany
ChasOneThrough
DbelongsTo
Attempts:
3 left
💡 Hint
Common Mistakes
Using belongsToMany instead of hasManyThrough
Using hasOneThrough which is for one-to-one relationships
Using belongsTo which is for inverse one-to-one or many-to-one
2fill in blank
medium

Complete the code to specify the foreign key on the intermediate model in a has-many-through relationship.

Laravel
return $this->hasManyThrough(Post::class, Country::class, '[1]');
Drag options to blanks, or click blank then click option'
Aid
Buser_id
Cpost_id
Dcountry_id
Attempts:
3 left
💡 Hint
Common Mistakes
Using the foreign key of the final model instead of the intermediate model
Using the primary key 'id' instead of a foreign key
3fill in blank
hard

Fix the error in the has-many-through relationship by completing the foreign key on the final model.

Laravel
return $this->hasManyThrough(Post::class, Country::class, 'country_id', '[1]');
Drag options to blanks, or click blank then click option'
Acountry_id
Buser_id
Cpost_id
Did
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same foreign key as the intermediate model
Using the primary key 'id' instead of a foreign key
4fill in blank
hard

Fill both blanks to complete the has-many-through relationship with custom local keys.

Laravel
return $this->hasManyThrough(Post::class, Country::class, '[1]', '[2]', 'id', 'id');
Drag options to blanks, or click blank then click option'
Acountry_id
Buser_id
Cpost_id
Dauthor_id
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up the order of foreign keys
Using primary keys instead of foreign keys
5fill in blank
hard

Fill all three blanks to complete the has-many-through relationship with all custom keys.

Laravel
return $this->hasManyThrough(Post::class, Country::class, '[1]', '[2]', '[3]');
Drag options to blanks, or click blank then click option'
Acountry_id
Buser_id
Cid
Dauthor_id
Attempts:
3 left
💡 Hint
Common Mistakes
Using default keys without specifying when custom keys are needed
Mixing up the order of parameters