0
0
Laravelframework~10 mins

Has-many-through in Laravel - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Has-many-through
Start: User Model
HasManyThrough Relation Defined
Query: User -> Posts Through Country
Laravel Builds SQL Join
Fetch Posts Related to User via Country
Return Collection of Posts
Shows how Laravel uses a has-many-through relation to get related records through an intermediate model.
Execution Sample
Laravel
class User extends Model {
  public function posts() {
    return $this->hasManyThrough(Post::class, Country::class);
  }
}

$posts = User::find(1)->posts;
Defines a has-many-through relation from User to Post through Country, then fetches posts for user with ID 1.
Execution Table
StepActionQuery BuiltResult
1Find User with ID 1SELECT * FROM users WHERE id = 1User model instance with id=1
2Call posts() relationBuild hasManyThrough SQL joinPrepare SQL to join countries and posts via user
3Execute SQL to get postsSELECT posts.* FROM posts INNER JOIN countries ON countries.id = posts.country_id WHERE countries.user_id = 1Collection of Post models related to User 1 through Country
4Return posts collectionN/APosts related to User 1
💡 All posts related to User 1 through their countries are fetched and returned.
Variable Tracker
VariableStartAfter Step 1After Step 3Final
$usernullUser(id=1)User(id=1)User(id=1)
$postsnullnullCollection of Post modelsCollection of Post models
Key Moments - 2 Insights
Why does Laravel join countries and posts tables in the query?
Because has-many-through needs to get posts related to the user through the intermediate country model, so it joins countries and posts tables to find matching posts.
What does the hasManyThrough method parameters mean?
The first parameter is the final related model (Post), the second is the intermediate model (Country) through which the relation passes, as shown in the execution_table step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what SQL query is run to fetch posts?
ASELECT * FROM posts WHERE user_id = 1
BSELECT * FROM countries WHERE user_id = 1
CSELECT posts.* FROM posts INNER JOIN countries ON countries.id = posts.country_id WHERE countries.user_id = 1
DSELECT posts.* FROM posts WHERE posts.country_id = 1
💡 Hint
Check step 3 in the execution_table for the exact SQL query.
At which step is the User model instance retrieved?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look at the action column in execution_table step 1.
If the intermediate model was changed from Country to City, what changes in the execution_table?
AThe SQL join would be between cities and posts instead of countries and posts
BThe user retrieval query would change
CThe posts query would not use any join
DNo changes would happen
💡 Hint
Refer to step 3 where the SQL join involves the intermediate model.
Concept Snapshot
Has-many-through relation in Laravel:
- Used to access distant related models through an intermediate model.
- Syntax: hasManyThrough(FinalModel::class, IntermediateModel::class)
- Laravel builds SQL joins automatically.
- Useful for relations like User -> Posts through Country.
- Returns a collection of related models.
Full Transcript
This visual execution shows how Laravel's has-many-through relation works. First, the User model instance is retrieved by ID. Then, calling the posts() relation triggers Laravel to build a SQL join query joining the intermediate Country model and the final Post model. The SQL query fetches posts related to the user through countries. Finally, Laravel returns a collection of Post models. Variables like $user and $posts change from null to model instances as the steps progress. Key points include understanding the SQL join and the meaning of the hasManyThrough parameters. The quizzes test understanding of the SQL query, step order, and effect of changing the intermediate model.