Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have the following route defined in my web.php, to return a list of my Excepcion records in DB:

use App\Excepcion;
use App\Http\Resources\Excepcion as ExcepcionResource;
(...)
Route::get('/list/excepciones', function () {
    return ExcepcionResource::collection(Excepcion::where('active', '1'));
});

But I'm getting the following server error:

Call to undefined method Illuminate\Database\Eloquent\Builder::mapInto()

If I change it to Excepcion::all() it works just fine. So, I'm not sure why I cannot use a where condition nor a scope. How can I filter the results here?

like image 463
Jk33 Avatar asked Oct 22 '18 22:10

Jk33


2 Answers

You forgot to call get to obtain the collection like so:

return ExcepcionResource::collection(Excepcion::where('active', '1')->get());
like image 84
Paras Avatar answered Oct 19 '22 07:10

Paras


For the record, if you call a relation in your resourceCollection (or anywhere else), don't forget that you must omit the parenthesis:

MyResource::collection($this->relation);

not

MyResource::collection($this->relation());

because the relation is not a method of the model, but a property.

like image 34
pvledoux Avatar answered Oct 19 '22 06:10

pvledoux