How to retrieve a result set from a raw query as an array instead of an object in Laravel 3

Ramesh picture Ramesh · Dec 6, 2012 · Viewed 61.8k times · Source

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] => [email protected]
            [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] => [email protected]
            [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] => [email protected]
                [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] => [email protected]
                [created_at] => 2012-12-06 00:00:00
                [updated_at] => 2012-12-05 00:00:00
            )
    )

Answer

Sinan Eldem picture Sinan Eldem · Dec 6, 2012

You may also get all the result always as array by changing

application/config/database.php

'fetch' => PDO::FETCH_CLASS,

on line 31 to

'fetch' => PDO::FETCH_ASSOC,