Skip to content

Data is not converted into array when with the DB facade. #53658

Closed Answered by rodrigopedra
tamdaz asked this question in Q&A
Discussion options

You must be logged in to vote

That is by design.

When you call Collection@toArray(), the collection instance iterates over its elements and checks if they implement the Illuminate\Support\Traits\Arrayable interface. If so, it calls that element's toArray() method.

When you query your DBMS through Eloquent, the result set is hydrated into Eloquent models. Those instances implement the Illuminate\Support\Traits\Arrayable, and thus know how to convert themselves into arrays when needed.

When you query your DBMS through the DB façade, the result set is not changed, and by default each record will be an instance of PHP's built-in \stdClass, and as such, those do not implement any of Laravel's interfaces.

So, when the colle…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by tamdaz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #53649 on November 25, 2024 03:23.