📖 Scenario: You are building a simple blog application using Laravel. Each blog post has an author and multiple comments. To improve performance, you want to load the posts along with their authors and comments in one query.
🎯 Goal: Build a Laravel query that uses eager loading with with to load posts together with their related authors and comments.
📋 What You'll Learn
Create a Post model instance query
Add eager loading for the author relationship
Add eager loading for the comments relationship
Retrieve all posts with their authors and comments in one query
💡 Why This Matters
🌍 Real World
Eager loading helps reduce the number of database queries when loading related data, making web applications faster and more efficient.
💼 Career
Understanding eager loading is essential for Laravel developers to write performant database queries and build scalable applications.
Progress0 / 4 steps
1
Create the base query for posts
Create a variable called posts and assign it the query builder for the Post model using Post::query().
Laravel
Hint
Use Post::query() to start building a query for posts.
2
Add eager loading for the author relationship
Add eager loading for the author relationship to the $posts query using the with method.
Laravel
Hint
Use with('author') to eager load the author relationship.
3
Add eager loading for the comments relationship
Extend the $posts query to eager load both author and comments relationships by passing an array to the with method.
Laravel
Hint
Pass an array ['author', 'comments'] to with to eager load both relationships.
4
Retrieve all posts with eager loaded relationships
Complete the query by calling get() on $posts to retrieve all posts with their authors and comments.
Laravel
Hint
Call get() to execute the query and get the results.
Practice
(1/5)
1. What is the main purpose of using with() in Laravel Eloquent queries?
easy
A. To delete related models automatically
B. To load related models in the same query to reduce database calls
C. To paginate the results of a query
D. To sort the results by a related model's attribute
Solution
Step 1: Understand what with() does
The with() method tells Laravel to load related models eagerly, meaning it fetches them in the same database query or fewer queries.
Step 2: Identify the benefit of eager loading
This reduces the number of database queries, improving performance by avoiding the N+1 query problem.
Final Answer:
To load related models in the same query to reduce database calls -> Option B
Assuming the 'customer' relation is defined correctly, what is the likely problem?
medium
A. Using with() after get() instead of before
B. Missing parentheses on customer relation call
C. Accessing customer without checking if it exists
D. Using get() instead of first()
Solution
Step 1: Check eager loading usage
The with('customer') is correctly used before get(), so related customers are loaded.
Step 2: Identify potential null relation access
Some orders might not have a customer, so $order->customer could be null, causing an error when accessing name without a check.
Final Answer:
Accessing customer without checking if it exists -> Option C
Quick Check:
Null relation access causes error, check before use [OK]
Hint: Always check if related model exists before accessing properties [OK]
Common Mistakes:
Thinking with() must come after get()
Assuming relation always exists
Confusing get() and first() usage
5. You want to eager load the 'comments' and their 'author' for each post efficiently. Which code correctly achieves this?
hard
A. Post::with(['comments.author'])->get();
B. Post::load('comments.author')->get();
C. Post::with(['comments', 'author'])->get();
D. Post::with('comments', 'author')->get();
Solution
Step 1: Understand nested eager loading syntax
To eager load a relation of a relation, use dot notation inside an array: with(['comments.author']).
Step 2: Analyze each option
Post::with('comments', 'author')->get(); loads comments and author separately, but author is not a direct relation of Post. Post::load('comments.author')->get(); uses load() statically, which is invalid as load() is an instance method. Post::with(['comments', 'author'])->get(); loads comments and author separately as array elements, but author is not direct relation. Post::with(['comments.author'])->get(); correctly loads comments and their author nested.
Final Answer:
Post::with(['comments.author'])->get(); -> Option A
Quick Check:
Nested relations use dot notation inside array [OK]
Hint: Use dot notation inside array for nested eager loading [OK]
Common Mistakes:
Passing multiple relations as separate strings without array
Using load() statically
Trying to load unrelated models directly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.