Laravel. Get data from pivot table

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Laravel: Efficiently Getting Data from Pivot Tables

As senior developers working with Laravel, we often deal with many-to-many relationships, which are fundamentally managed by pivot tables. While Eloquent makes defining these relationships incredibly clean, extracting specific data—especially just the IDs from the pivot table—can sometimes feel like navigating unnecessary complexity.

The question we are tackling today is: How can I get data from a pivot table directly (with Laravel) without retrieving the entire related model collection?

The Pitfall of Default Eloquent Loading

You've correctly identified the standard way to access a many-to-many relationship using belongsToMany. When you call $order->related_orders, Eloquent executes a query to fetch all related models. If your goal is only to retrieve the foreign keys stored in the pivot table (e.g., only the related_id values), loading entire model instances is inefficient and wastes memory and database resources.

As you noted, iterating through the resulting collection just to extract an ID is irrational performance-wise. We need a method that targets the join data directly.

Solution 1: Attaching Pivot Data with withPivot()

The first step in leveraging pivot data within Eloquent is using the withPivot() method. This tells Eloquent to include the extra columns from the pivot table in the resulting query, without necessarily loading the full related model objects if you don't need them immediately.

If you want to see the pivot information alongside your main model:

$order = \App\Model\Orders::withPivot('related_id', 'pivot_data')->find(1);

// Accessing the pivot data directly:
echo $order->pivot->related_id; // This will contain the related_id from the pivot table

While this is better than loading full models, it still loads the relationship structure. For scenarios where you truly only need an aggregation of IDs, we can go further by leveraging query constraints.

Solution 2: The Efficient Approach – Direct Querying with Joins

When the requirement is strictly to retrieve a list of related IDs based on an existing record, the most efficient method often involves using Eloquent's powerful query building capabilities or, for maximum performance on large datasets, direct database queries (which you mentioned as an option).

Using Relationship Constraints (whereHas)

If you want to find all Order records that have a specific related order ID (a subset of the pivot table data), you use whereHas. This keeps the query focused on filtering relationships rather than loading full object graphs.

// Find all orders that are related to Order ID 5
$orders = \App\Model\Orders::whereHas('relatedOrders', function ($query) {
    $query->where('related_id', 5); // Filtering the pivot table directly
})->get();

The Most Direct Way: Using with() for Aggregation (The Eloquent Optimization)

If you need to collect only the related IDs from a single parent record, you can still use eager loading, but structure the retrieval to target only the necessary fields.

To get an array of just the pivot IDs for Order ID 1:

$order = \App\Model\Orders::with('related_orders')
                  ->find(1);

$relatedIds = [];
if ($order && $order->related_orders) {
    foreach ($order->related_orders as $pivotRow) {
        // Access the pivot data directly from the collection elements
        $relatedIds[] = $pivotRow->related_id; 
    }
}

// $relatedIds now contains an array of only the related IDs.

While this still involves a loop, it is now querying pre-loaded relationships rather than executing separate queries for every single pivot entry, making it significantly cleaner and often more performant within the Laravel ecosystem when dealing with relational data structure, keeping in mind how robust Eloquent's architecture is designed to handle these complex joins.

Conclusion: Choosing the Right Tool

The choice between an Eloquent approach (like using withPivot) and a raw SQL query depends entirely on the context.

  1. For simple data retrieval or when you need related models: Stick to Eloquent relationships (belongsToMany, withPivot).
  2. For complex filtering, aggregation, or when maximum raw performance is critical (especially across massive pivot tables): Direct database queries using joins or raw expressions are perfectly valid and can offer superior speed.

As we build scalable applications on Laravel, understanding how to optimize data fetching—knowing when to leverage Eloquent features versus diving into the SQL layer—is key to writing high-performance code. For deeper insights into the architecture powering these efficient solutions, exploring resources from laravelcompany.com is always recommended.