📖 Scenario: You are building a simple Laravel application to display a list of orders along with the customer names who placed them. The data is stored in two tables: customers and orders. You want to join these tables using Laravel's Eloquent ORM to fetch the combined data.
🎯 Goal: Build a Laravel Eloquent query that joins the orders table with the customers table on the customer_id field, and retrieves the order ID and the customer's name.
📋 What You'll Learn
Create an Eloquent model variable for the orders table
Define a variable for the join condition column name
Write an Eloquent query using join to combine orders and customers
Select the orders.id and customers.name fields in the query
💡 Why This Matters
🌍 Real World
Joining tables is common in web apps to combine related data, like showing orders with customer names.
💼 Career
Understanding Eloquent joins is essential for backend Laravel developers to write efficient database queries.
Progress0 / 4 steps
1
Set up the Orders model variable
Create a variable called $orders and assign it to the Eloquent model Order::query() to start building the query.
Laravel
Hint
Use Order::query() to get a new query builder instance for the orders table.
2
Define the join column variable
Create a variable called $joinColumn and set it to the string 'customer_id', which will be used as the join key between orders and customers.
Laravel
Hint
Assign the exact string 'customer_id' to the variable $joinColumn.
3
Write the join query
Use the join method on $orders to join the customers table where orders.customer_id equals customers.id. Store the result back in $orders.
Laravel
Hint
Use join('customers', 'orders.' . $joinColumn, '=', 'customers.id') to join the tables.
4
Select the desired columns
Add a select method to $orders to retrieve only orders.id and customers.name. Store the final query back in $orders.
Laravel
Hint
Use select('orders.id', 'customers.name') to specify the columns to retrieve.
Practice
(1/5)
1. What does the join method do in Laravel's query builder?
easy
A. Creates a new table by merging two tables.
B. Returns all rows from the first table regardless of matches.
C. Deletes rows from the joined tables.
D. Combines rows from two tables where matching columns exist (inner join).
Solution
Step 1: Understand the purpose of join
The join method combines rows from two tables where the join condition matches, which is an inner join.
Step 2: Differentiate from other join types
Unlike leftJoin, join excludes rows without matches in the second table.
Final Answer:
Combines rows from two tables where matching columns exist (inner join). -> Option D
Quick Check:
Join = Inner join [OK]
Hint: Join means only matching rows combined [OK]
Common Mistakes:
Confusing join with leftJoin
Thinking join deletes data
Assuming join creates new tables
2. Which of the following is the correct syntax to perform an inner join between 'users' and 'posts' on 'users.id' and 'posts.user_id' in Laravel?
easy
A. $users = DB::table('users')->join('posts', 'users.id' == 'posts.user_id')->get();
B. $users = DB::table('users')->join('posts', 'users.id', 'posts.user_id')->get();
C. $users = DB::table('users')->join('posts', 'users.id', '=', 'posts.user_id')->get();
D. $users = DB::table('users')->join('posts')->on('users.id', '=', 'posts.user_id')->get();
Solution
Step 1: Check the join method parameters
The correct join method requires four parameters: table, first column, operator, second column.
Step 2: Validate the syntax
$users = DB::table('users')->join('posts', 'users.id', '=', 'posts.user_id')->get(); correctly uses join('posts', 'users.id', '=', 'posts.user_id'). Other options miss operator or use invalid syntax.
Final Answer:
$users = DB::table('users')->join('posts', 'users.id', '=', 'posts.user_id')->get(); -> Option C
3. Given the following Laravel query, what will be the output if the 'users' table has 3 users and the 'posts' table has 2 posts linked to user IDs 1 and 2?