Bird
Raised Fist0
Laravelframework~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of the hasManyThrough relationship in Laravel?
easy
A. To eager load unrelated models
B. To define a direct one-to-one relationship
C. To create a many-to-many relationship with a pivot table
D. To access related models through an intermediate model

Solution

  1. Step 1: Understand relationship types

    Laravel offers different relationships like one-to-one, many-to-many, and has-many-through for specific data access patterns.
  2. Step 2: Identify hasManyThrough purpose

    The hasManyThrough relationship allows accessing distant related models through an intermediate model, simplifying queries.
  3. Final Answer:

    To access related models through an intermediate model -> Option D
  4. Quick Check:

    hasManyThrough = access through intermediate model [OK]
Hint: Think: two-step relation via a middle model [OK]
Common Mistakes:
  • Confusing hasManyThrough with many-to-many
  • Using it for direct one-to-one relations
  • Assuming it loads unrelated models
2. Which of the following is the correct syntax to define a hasManyThrough relationship in a Laravel model?
easy
A. return $this->belongsToMany(Target::class, Intermediate::class);
B. return $this->hasManyThrough(Target::class, Intermediate::class);
C. return $this->hasOneThrough(Target::class, Intermediate::class);
D. return $this->hasMany(Target::class);

Solution

  1. Step 1: Recall method signatures

    The hasManyThrough method requires the target model and the intermediate model as parameters.
  2. Step 2: Match correct syntax

    return $this->hasManyThrough(Target::class, Intermediate::class); correctly uses hasManyThrough(Target::class, Intermediate::class), matching Laravel's pattern.
  3. Final Answer:

    return $this->hasManyThrough(Target::class, Intermediate::class); -> Option B
  4. Quick Check:

    hasManyThrough syntax = hasManyThrough(Target, Intermediate) [OK]
Hint: Remember: hasManyThrough needs target and intermediate classes [OK]
Common Mistakes:
  • Using belongsToMany instead of hasManyThrough
  • Confusing hasOneThrough with hasManyThrough
  • Omitting the intermediate model parameter
3. Given these models:
class Country extends Model { public function posts() { return $this->hasManyThrough(Post::class, User::class); } }
And the database has:
- Country with id=1
- Users with country_id=1
- Posts with user_id matching those users

What will Country::find(1)->posts return?
medium
A. All posts regardless of country
B. All users from country with id 1
C. All posts written by users from country with id 1
D. An empty collection

Solution

  1. Step 1: Understand the hasManyThrough chain

    The posts() method on Country accesses posts through users, linking country -> users -> posts.
  2. Step 2: Trace the query result

    Calling Country::find(1)->posts fetches posts where the post's user belongs to users with country_id=1.
  3. Final Answer:

    All posts written by users from country with id 1 -> Option C
  4. Quick Check:

    hasManyThrough fetches distant related posts [OK]
Hint: Think: posts linked through users of country 1 [OK]
Common Mistakes:
  • Expecting users instead of posts
  • Ignoring the intermediate user model
  • Assuming posts from all countries
4. Consider this incorrect hasManyThrough definition:
public function posts() { return $this->hasManyThrough(Post::class); }

What is the main issue here?
medium
A. Missing the intermediate model class as second argument
B. Using hasManyThrough instead of belongsToMany
C. The method should return hasOneThrough
D. The target model class is incorrect

Solution

  1. Step 1: Check method parameters

    The hasManyThrough method requires two arguments: the target model and the intermediate model.
  2. Step 2: Identify missing argument

    The code only passes Post::class but omits the intermediate model, causing an error.
  3. Final Answer:

    Missing the intermediate model class as second argument -> Option A
  4. Quick Check:

    hasManyThrough needs target and intermediate models [OK]
Hint: Always pass two models: target and intermediate [OK]
Common Mistakes:
  • Forgetting the intermediate model argument
  • Confusing hasManyThrough with belongsToMany
  • Assuming one argument is enough
5. You have three models: Country, User, and Post. Users belong to countries, and posts belong to users.

How would you define a hasManyThrough relationship in the Country model to get all posts written by users from that country, assuming foreign keys are nation_id in users table, author_id in posts table?
hard
A. return $this->hasManyThrough(Post::class, User::class, 'nation_id', 'author_id');
B. return $this->hasManyThrough(Post::class, User::class, 'user_id', 'country_id');
C. return $this->hasManyThrough(User::class, Post::class, 'country_id', 'user_id');
D. return $this->hasManyThrough(Post::class, User::class);

Solution

  1. Step 1: Understand parameter order in hasManyThrough

    The method signature is hasManyThrough(Target, Intermediate, foreignKeyOnIntermediate, foreignKeyOnTarget).
  2. Step 2: Match keys to models

    Users have nation_id linking to Country, and Posts have author_id linking to User, so keys are 'nation_id' and 'author_id' respectively.
  3. Step 3: Choose correct option

    return $this->hasManyThrough(Post::class, User::class, 'nation_id', 'author_id'); correctly passes Post::class, User::class, 'nation_id', and 'author_id' in order.
  4. Final Answer:

    return $this->hasManyThrough(Post::class, User::class, 'nation_id', 'author_id'); -> Option A
  5. Quick Check:

    hasManyThrough keys = intermediate FK, target FK [OK]
Hint: Order keys: intermediate FK, then target FK [OK]
Common Mistakes:
  • Swapping foreign key order
  • Using wrong model classes order
  • Omitting foreign keys when needed
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard