Laravel 5: Querying a many to many relationship without "pivot" in the result

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Laravel: Querying Many-to-Many Relationships Without Including Pivot Data

As a senior developer working with Laravel, we frequently deal with many-to-many relationships. Eloquent makes setting up these relationships incredibly clean using pivot tables, but sometimes, when querying the relationship, we don't want to load the entire related model structure or the internal pivot data—we just need the foreign keys.

This post addresses a common pain point: how to retrieve only the related IDs from a many-to-many relationship without cluttering your results with the unnecessary pivot details.

The Scenario: Many-to-Many Data Retrieval

Let's establish the context based on the provided schema: we have users, books, and a pivot table favourite_books linking them.

The goal is to fetch all the book_ids associated with a specific user, but ideally, only return an array of these IDs, not an array of Eloquent objects containing the pivot information.

Your current approach using $user->favouriteBooks()->select('book_id')->get(); is a good start, but as you observed, it still returns the result wrapped in the model structure, including the pivot details:

[
   {
     "book_id": 23,
     "pivot": {
       "user_id": 57,
       "book_id": 23
     }
   },
   // ... more results
]

We want the cleaner output: [23, 41, ...]

Solution 1: Querying the Pivot Table Directly (The Most Performant Way)

When you only need simple foreign key IDs from a pivot table, bypassing the Eloquent relationship loading mechanism and querying the pivot table directly is often the most efficient method. This leverages the raw power of SQL when performance is paramount.

To achieve this, we query the favourite_books table directly, filtering by the user_id, and select only the desired foreign key (book_id).

Here is how you would implement this to get just the book IDs for a specific user:

use App\Models\User;

class UserController extends Controller
{
    public function getUserFavouriteBookIds(User $user)
    {
        // Directly query the pivot table for the relevant book_ids
        $bookIds = \DB::table('favourite_books')
                     ->where('user_id', $user->id)
                     ->pluck('book_id'); // pluck() returns a simple collection of the selected column values

        return $bookIds->toArray();
    }
}

Why this works: This method executes a highly optimized SQL query directly against the database. It avoids the overhead of hydrating Eloquent models or loading complex relationship definitions, making it extremely fast, especially when dealing with large datasets. This principle aligns perfectly with efficient data access that Laravel promotes.

Solution 2: Refining the Eloquent Approach (Using with and Selection)

While Solution 1 is excellent for raw performance, sometimes keeping the query within the Eloquent relationship structure is desirable for code readability. We can refine your original approach to explicitly select only the columns we need from the related models.

If you start with the relationship method, you can use nested selection techniques. However, since belongsToMany returns a collection of related models, we must select from those results:

use App\Models\User;

class UserController extends Controller
{
    public function getUserFavouriteBooksEloquent(User $user)
    {
        // Fetch the relationship and explicitly select only the 'book_id' from the related Books model.
        $bookIds = $user->favouriteBooks()
                       ->select('books.id') // Select the ID column from the related books table
                       ->pluck('books.id'); // Retrieve just the IDs

        return $bookIds->toArray();
    }
}

Explanation: In this approach, instead of selecting only book_id (which is technically part of the pivot structure when using Eloquent relationships), we select from the related model (books.*) and then use pluck() to extract just the IDs. This keeps the logic within the Eloquent relationship context while achieving the desired flat array result.

Conclusion: Choosing the Right Tool

Both solutions successfully solve the problem of retrieving only the book IDs without including unnecessary pivot data. However, the choice depends on your specific requirements:

  1. For Maximum Performance: Use Solution 1 (Direct Pivot Query) when you only need a list of foreign keys and raw speed is critical.
  2. For Eloquent Consistency: Use Solution 2 (Refined Eloquent Query) when you prefer to keep the data access logic encapsulated within your models, leveraging the power of Laravel's Eloquent ORM.

As you build complex applications on Laravel, understanding how to optimize database interactions—knowing when to use raw queries versus Eloquent methods—is crucial for writing scalable and high-performing code. For more advanced insights into efficient querying patterns in Laravel, always refer back to the official documentation at laravelcompany.com.