How to get data from related table in Laravel (one to many)?
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
How to Get Data from Related Tables in Laravel: Mastering One-to-Many Relationships
As developers working with relational databases, navigating relationships between tables is fundamental. In Laravel, this interaction is elegantly handled by Eloquent ORM, which allows you to treat database relationships as actual object relationships. When dealing with a one-to-many scenario—like fetching all orders for a specific user—it’s easy to run into issues if you don't use the right loading techniques.
This post will walk you through the common pitfalls when querying related data and provide the robust, efficient solutions using Laravel Eloquent, ensuring your data retrieval is clean, fast, and accurate.
Understanding the One-to-Many Relationship
The scenario you described involves a classic one-to-many relationship: one user can have many orders. In Eloquent, this is defined by defining relationships on both models.
First, let's solidify the model setup for our User and Order models:
// app/Models/User.php
class User extends Authenticatable
{
public function orders()
{
// Defines that a user has many orders
return $this->hasMany(Order::class);
}
}
// app/Models/Order.php
class Order extends Model
{
public function user()
{
// Defines that an order belongs to one user
return $this->belongsTo(User::class);
}
}
These methods allow Eloquent to automatically map the foreign keys (user_id in the orders table) to the primary keys (id in the users table). This is the backbone of database interaction in Laravel, making complex joins feel like simple object calls.
The Pitfall: Why Your Query Returned NULL
You encountered an issue when attempting to fetch related data, resulting in NULL results. This often happens because you are trying to perform a standard where query on one model and then immediately try to access the related model without explicitly telling Eloquent how to load that relationship.
Consider your attempt:
$orders = Order::where('user_id', Auth::guard('api')->id())->get();
// Attempting to get $orders->user here will fail or return null,
// because the 'Order' model only knows how to find its direct parent (belongsTo),
// not necessarily load the entire User object efficiently in this context.
The problem isn't usually that the relationship doesn't exist; it’s that fetching data across tables requires specific instructions for efficiency. If you perform multiple queries (one for orders, then potentially another to fetch the user details), you introduce performance bottlenecks known as the N+1 query problem.
The Solution: Mastering Eager Loading with with()
The most efficient way to solve this is by using Eager Loading. Eager loading instructs Eloquent to load all necessary related data in advance, typically using optimized JOIN operations or separate, highly optimized queries, preventing the N+1 problem.
To get the user details along with their associated orders efficiently, you should start from the User model and eager load the relationship:
use App\Models\User;
use Illuminate\Support\Facades\Auth;
public function index()
{
$user = Auth::guard('api')->user;
// Use with('orders') to fetch the user AND all their related orders in one or two efficient queries.
$userWithOrders = User::with('orders')->find($user->id);
return response()->json($userWithOrders);
}
Alternative: Querying from the Parent Model
If your primary goal is to list users and see their orders, you can structure the query to start with the User model, which is often cleaner when dealing with one-to-many structures.
use App\Models\User;
public function index()
{
$user = Auth::guard('api')->user;
// Fetch the user and immediately load their orders using relationship access.
$user->load('orders');
return response()->json($user);
}
By using $user->load('orders'), Eloquent executes a single, optimized query (or two highly efficient queries) to retrieve the user data and all associated order data in one go. This pattern demonstrates how powerful relationships are when leveraged correctly, aligning perfectly with the principles of efficient data handling championed by frameworks like Laravel.
Conclusion
Getting data from related tables in Laravel is not about manually writing complex SQL joins; it’s about trusting Eloquent to handle the complexity for you. For one-to-many relationships, always favor Eager Loading using the with() method when you anticipate needing the related data alongside your primary model. Mastering this concept will lead you to more performant, cleaner, and highly maintainable codebases.