Laravel API ResourceCollection - Call to undefined method Illuminate\Database\Eloquent\Builder::mapInto()

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Laravel API ResourceCollection Error: Understanding Eloquent Builders and Collections

As senior developers working with the Laravel ecosystem, we frequently encounter subtle yet frustrating errors when bridging the gap between Eloquent Query Builders and API resource transformations. Today, we’re diving into a specific issue that many developers face when trying to return filtered data through an API endpoint: the error Call to undefined method Illuminate\Database\Eloquent\Builder::mapInto().

This post will diagnose why this error appears, explain the correct way to filter Eloquent results before passing them to a Resource Collection, and provide robust solutions.

The Problem: Builders vs. Collections

You have correctly identified the setup: you are trying to use an Eloquent Query Builder (via where()) inside a route closure that expects an API Resource collection.

Here is the problematic code snippet context:

Route::get('/list/excepciones', function () {
    return ExcepcionResource::collection(Excepcion::where('active', '1')); // <-- Error occurs here
});

When you use methods like where(), orderBy(), or with() on an Eloquent model, they return an instance of the Illuminate\Database\Eloquent\Builder. This Builder is a blueprint for a database query; it has not yet executed the query to fetch the actual results.

The error Call to undefined method Illuminate\Database\Eloquent\Builder::mapInto() suggests that the framework or your specific setup expects something different than a raw, unexecuted Builder object when feeding it into the Resource collection mechanism. While Eloquent builders are powerful, they must be explicitly instructed on how to resolve themselves into data before being processed by API layers like Resources.

Why Excepcion::all() Works, But where() Fails

The reason Excepcion::all() works smoothly is that the all() method immediately executes the query and returns a collection of models. When you pass this result to ExcepcionResource::collection(), you are passing an actual Eloquent Collection object, which the Resource layer knows exactly how to handle.

When you use where('active', '1'), you are chaining methods on the Builder. In certain contexts, trying to pass the live Builder object directly results in method resolution failures within the API pipeline, leading to the "undefined method" error because the necessary execution steps haven't been completed yet.

The Solution: Executing the Query Explicitly

The solution is straightforward: you must tell the Eloquent Builder to execute the query and retrieve the data before passing it to the Resource collection. You can do this by calling a method like get() or get() on the builder immediately after applying your constraints.

Here is the corrected implementation:

use App\Excepcion;
use App\Http\Resources\Excepcion as ExcepcionResource;

Route::get('/list/excepciones', function () {
    // Execute the query using get() to fetch the results into a Collection
    $excepcions = Excepcion::where('active', '1')->get();
    
    return ExcepcionResource::collection($excepcions);
});

Best Practice: Using Query Scopes for Cleaner Code

While the above solution fixes the immediate error, it’s important to remember that complex filtering logic should be kept separate from your route definitions. A much cleaner and more maintainable approach is to utilize Eloquent Local Scopes. Local scopes allow you to encapsulate reusable query constraints directly on your model, making your controller logic extremely clean.

For instance, you could define a scope on the Excepcion model:

// In App\Excepcion.php
public function scopeActive($query)
{
    return $query->where('active', '1');
}

Then your route becomes significantly cleaner:

Route::get('/list/excepciones', function () {
    // The query is now highly readable and declarative
    $excepcions = Excepcion::active()->get(); 
    
    return ExcepcionResource::collection($excepcions);
});

This approach aligns perfectly with Laravel's philosophy of building expressive, robust applications. As you manage larger API structures, leveraging these Eloquent features is crucial for maintaining clean separation between data logic and presentation layers, which is a core principle supported by the entire Laravel framework, including best practices discussed on the Laravel Company website.

Conclusion

The error you encountered stems from attempting to pass an unexecuted Eloquent Query Builder object directly into a method expecting a concrete collection of data. By explicitly calling methods like get() on your builder, you force the database query to execute, fetch the results, and return a standard Laravel Collection, which is exactly what the Resource::collection() method expects. Always ensure your data retrieval steps are fully materialized before passing them to API presentation layers for reliable performance and stability.