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?
You forgot to call get
to obtain the collection like so:
return ExcepcionResource::collection(Excepcion::where('active', '1')->get());
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With