0
0
Laravelframework~10 mins

Eager loading (with) in Laravel - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Eager loading (with)
Start Query
Specify Relations with with()
Build SQL for Main Model
Build SQL for Related Models
Execute Queries
Combine Results
Return Models with Relations Loaded
This flow shows how Laravel builds and executes queries to load main models and their related models together using eager loading.
Execution Sample
Laravel
User::with('posts')->get();
This code loads all users and their related posts in one go to avoid extra queries later.
Execution Table
StepActionQuery BuiltResultNotes
1Start query for User modelSELECT * FROM usersAll users fetchedMain model query prepared
2Identify relation 'posts'SELECT * FROM posts WHERE user_id IN (user ids)Posts for all users fetchedRelated model query prepared
3Combine users and postsN/AUsers with posts attachedEager loading combines data
4Return resultN/ACollection of users with postsReady for use without extra queries
5EndN/AQuery completeNo lazy loading needed
💡 All related posts loaded with users, avoiding multiple queries per user.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
usersemptyall users from DBall users from DBusers with posts attachedusers with posts attached
postsemptyemptyall posts for usersall posts for usersall posts for users
Key Moments - 2 Insights
Why does Laravel run two queries instead of one when using with('posts')?
Laravel runs one query for users and one for posts with a WHERE IN clause to efficiently load all related posts at once, as shown in execution_table rows 1 and 2.
What happens if we don't use with() and access posts later?
Without with(), Laravel runs a new query for posts each time you access posts on a user, causing many queries. Eager loading avoids this by loading all posts upfront (see execution_table row 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what query is built at step 2?
ASELECT * FROM users
BSELECT * FROM posts
CSELECT * FROM posts WHERE user_id IN (user ids)
DSELECT * FROM users WHERE id IN (post ids)
💡 Hint
Check the 'Query Built' column in row 2 of the execution_table.
At which step are users combined with their posts?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at the 'Action' column describing combining results in the execution_table.
If we remove with('posts'), how would the variable 'posts' change after step 2?
AIt would contain all posts for users
BIt would remain empty
CIt would contain posts for only one user
DIt would cause an error
💡 Hint
Refer to variable_tracker row for 'posts' and understand eager loading effect.
Concept Snapshot
Eager loading with with() loads related models in advance.
Syntax: Model::with('relation')->get();
Runs separate queries for main and related models.
Combines results to avoid many queries later.
Improves performance by reducing database calls.
Full Transcript
Eager loading in Laravel uses the with() method to load related models together with the main model. When you call User::with('posts')->get(), Laravel builds one query to get all users and another query to get all posts for those users using a WHERE IN clause. Then it combines the posts with their respective users before returning the result. This avoids running a new query for posts each time you access them on a user, which would happen without eager loading. The process involves starting the main query, preparing the related query, executing both, and combining the data. This makes your app faster and more efficient by reducing the number of database queries.