Skip to content
Advertisement

How to use a resource collection toArray and DB (without Eloquent)

I have this query (in a repository, without using Eloquent):

JavaScript

I have this controller:

JavaScript

And at least, I have this collection:

JavaScript

I have this error 500:

“message”: “Call to undefined method stdClass::toArray()”,

I think it is because I use the ‘DB’ facade instead of Eloquent. So how to work with ‘DB’ facade and collection?

Advertisement

Answer

Yes, because query builder return a collection with stdClass inside.

And ResourceCollection‘s method toArray will map the collection, then run the toarray() on stdClass. So error occurs.

However, eloquent builder collection will map the collection, run the toArray() on model’s object. And this object has toArray() method.

Here is the source code:

JavaScript

So you can rewrite the toArray() method for query builder in your resource:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement