0
0
Laravelframework~20 mins

Has-many-through in Laravel - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
HasManyThrough Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does the hasManyThrough relationship return?

Consider a Laravel model Country that has many Posts through User. What will $country->posts return?

Laravel
class Country extends Model {
  public function posts() {
    return $this->hasManyThrough(Post::class, User::class);
  }
}

$country = Country::find(1);
$posts = $country->posts;
AA collection of all posts written by users belonging to the country with ID 1.
BA collection of all users belonging to the country with ID 1.
CA single post belonging to the country with ID 1.
DAn error because hasManyThrough requires three parameters.
Attempts:
2 left
💡 Hint

Think about how hasManyThrough connects models through an intermediate model.

📝 Syntax
intermediate
1:30remaining
Identify the correct hasManyThrough method signature

Which of the following is the correct way to define a hasManyThrough relationship in Laravel?

Areturn $this->hasManyThrough(User::class, Post::class);
Breturn $this->hasManyThrough(Post::class, User::class);
Creturn $this->hasManyThrough(Post::class);
Dreturn $this->hasManyThrough(User::class);
Attempts:
2 left
💡 Hint

The first parameter is the final model, the second is the intermediate model.

🔧 Debug
advanced
2:30remaining
Why does this hasManyThrough query return no results?

Given the following code, why might $country->posts return an empty collection?

Laravel
class Country extends Model {
  public function posts() {
    return $this->hasManyThrough(Post::class, User::class, 'country_id', 'user_id');
  }
}

$country = Country::find(1);
$posts = $country->posts;
AThe foreign key names in the hasManyThrough method are incorrect or swapped.
BThe Post model does not have a user_id column.
CThe User model does not have a country_id column.
DThe Country model does not have a posts table.
Attempts:
2 left
💡 Hint

Check the order and names of foreign keys in the hasManyThrough method.

state_output
advanced
1:30remaining
What is the count of posts returned by hasManyThrough?

Assume the following data:

  • Country with ID 1 has 2 users.
  • User 1 has 3 posts.
  • User 2 has 2 posts.

What is the value of $country->posts->count()?

Laravel
class Country extends Model {
  public function posts() {
    return $this->hasManyThrough(Post::class, User::class);
  }
}

$country = Country::find(1);
$count = $country->posts->count();
A3
B2
C0
D5
Attempts:
2 left
💡 Hint

Count all posts from all users of the country.

🧠 Conceptual
expert
3:00remaining
Which statement about hasManyThrough is true?

Choose the correct statement about Laravel's hasManyThrough relationship.

AIt automatically eager loads all related models without specifying with().
BIt can only be used if the intermediate model has a one-to-one relationship with the parent model.
CIt allows accessing a model through an intermediate model using foreign keys on both intermediate and final models.
DIt requires the intermediate model to have a primary key named 'id' only.
Attempts:
2 left
💡 Hint

Think about how hasManyThrough connects models through two foreign keys.