By default, Laravel’s raw query methods return results as arrays of stdClass objects:
Array
(
[0] => stdClass Object
(
[id] => 1
[username] => admin
[password] => admin123
[email] => admin@admin.com
[created_at] => 2012-12-06 18:57:19
[updated_at] => 2012-12-06 00:00:00
)
[1] => stdClass Object
(
[id] => 2
[username] => userna
[password] => user
[email] => user@gmail.com
[created_at] => 2012-12-06 00:00:00
[updated_at] => 2012-12-05 00:00:00
)
)
The question is how to have Laravel return an array of Arrays instead:
Array
(
[0] => Array
(
[id] => 1
[username] => admin
[password] => admin123
[email] => admin@admin.com
[created_at] => 2012-12-06 18:57:19
[updated_at] => 2012-12-06 00:00:00
)
[1] => Array
(
[id] => 2
[username] => userna
[password] => user
[email] => user@gmail.com
[created_at] => 2012-12-06 00:00:00
[updated_at] => 2012-12-05 00:00:00
)
)
Original answer for Laravel 3
Eloquent has a method
to_array()From docs:
or
If you are using fluent, you can do as Sinan suggested and change the global configuration to return an associative array rather than objects.
Alternatively, you can convert an object to and from JSON to convert it to an array although the global option would be preferable in most cases. You can use the following in projects where you prefer objects normally but in certain edge cases need an array. This method will not play well with Eloquent, use the methods above in that case.
Another option would be to temporarily change the runtime configuration
For Laravel ~4
In Laravel 4 onwards, all method names conform to PSR-2 standards.