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

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

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Retrieving Raw Query Results as Arrays in Laravel 3

When executing raw SQL queries within Laravel, the framework defaults to returning results as an array of stdClass objects. This is the standard behavior for version 3 and earlier iterations of the framework. If you inspect the output of a query like DB::select('SELECT * FROM users'), you will see a structure where each row is instantiated as an anonymous class:

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
        )
)

While stdClass objects provide a convenient way to access columns via magic methods (e.g., $row->username), there are scenarios where you strictly require a native PHP array structure. For instance, if your downstream logic relies on specific array functions like array_column(), usort() with string keys, or serialization protocols that behave differently with objects versus arrays, the default return type might introduce friction.

To achieve an array of arrays instead of an array of objects, you must explicitly instruct the query builder to convert the results before they are returned from the database layer. In Laravel 3, this is accomplished by chaining the get() method with a specific option argument or by utilizing the underlying PDO fetch mode directly if interacting with the connection manually.

The most straightforward approach within the fluent interface of Laravel 3 is to pass an options array to the select or get command that forces the conversion. However, in strict raw query contexts using DB::select(), the framework does not always expose a simple boolean flag like ->asArray() without modifying the internal driver logic or upgrading to newer versions where this pattern became more explicit.

A robust workaround for Laravel 3 specifically involves accessing the raw PDO connection and configuring the fetch mode before executing the statement. This ensures that the database driver itself returns associative arrays rather than object representations. You can achieve this by retrieving the connection instance, setting the PDO::FETCH_ASSOC flag on the cursor options, and then fetching the data:

$conn = DB::connection();
$query = $conn->getPdo()->query('SELECT * FROM users');

// Set the fetch mode to return associative arrays instead of objects
$options = array(PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC);

$results = $query->fetchAll($options);

In this implementation, PDO::FETCH_ASSOC is the critical directive. It tells the underlying PDO driver to map columns to keys based on their names rather than creating an anonymous object with those properties. The resulting $results variable will contain exactly what you requested: a standard PHP array where every element is also an array, keyed by column name.

There are distinct trade-offs to consider when choosing between stdClass and native arrays. Objects carry slightly more memory overhead per row compared to simple arrays because of the internal class structure they instantiate. If you are processing thousands of rows in a tight loop or generating large serialized payloads, the performance difference might become measurable. Conversely, objects offer better encapsulation and allow for property access without needing to know the exact key names at runtime, which can be more maintainable if your SQL schema changes frequently.

Another potential pitfall is compatibility with other libraries. Some JSON serialization tools or reflection-based introspection tools handle stdClass differently than raw arrays. If you are passing this data to a third-party API that strictly expects JSON objects derived from PHP arrays, converting via PDO::FETCH_ASSOC ensures the structure matches expectations without needing an additional mapping step in your controller logic.

If you find yourself frequently needing this behavior for specific tables or complex joins, consider wrapping the raw query logic in a helper method. This encapsulates the PDO configuration details and keeps your main application code clean. Always verify the return type of your database calls, especially when upgrading frameworks, as Laravel 4 and later introduced different methods like fetch() with fetch mode options directly on the builder, making this specific workaround less necessary in modern versions.

Our network