Laravel 5.1 DB:select toArray()

Laravel 5.1 DB:select toArray()

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

Stefan Bogdanescu

Founder & Senior Architect

Laravel 5.1 DB:select toArray()

In Laravel 5.1, the DB::select method is a convenient way to execute raw SQL queries directly against your database connection. When you pass a query string to this method, as in $result = DB::select('select * from users');, the framework expects the driver (MySQL, PostgreSQL, SQLite) to return an array of result objects.

By default, these objects are instances of stdClass. This is the standard behavior for raw SQL execution in many database drivers because it preserves the column names as keys while keeping the data in a generic, associative array format that can be serialized easily. However, this structure often feels clunky when you need to manipulate the data immediately after retrieval. You might find yourself iterating over $result and accessing properties like $row->id, which requires type hinting or casting if your downstream logic expects strict arrays.

The core issue is that DB::select does not have a built-in method called toArray() in Laravel 5.1 to convert the collection of stdClass objects into a pure PHP array. Attempting to call $result->toArray() will throw an error because stdClass lacks that magic method, and the top-level result is already an array containing objects, not an object itself.

To achieve a structure where each row is represented as a simple associative array rather than an object, you must manually iterate through the results and convert them using the json_decode function or by accessing the internal properties directly. The most robust approach that mimics the behavior of Eloquent's toArray() method is to use json_encode followed by json_decode.

Here is how you can refactor your raw query result:

$result = DB::select('SELECT * FROM users');

// Convert stdClass objects to associative arrays
$usersArray = array_map(function ($row) {
    return (array)$row;
}, $result);

Alternatively, using json_encode and json_decode is often preferred because it ensures deep cloning of the data, preventing any accidental reference issues if you modify one row later:

$usersArray = json_decode(json_encode($result), true);

The second argument in json_decode, set to true, is crucial. It tells PHP to return an array of associative arrays instead of an array of objects, effectively flattening the structure into exactly what you requested.

There are specific trade-offs to consider when choosing between raw SQL and Eloquent queries. If your goal is simply to retrieve data as a flat array for processing, passing, or logging, converting DB::select output this way eliminates the overhead of maintaining custom classes for every database row. It also makes serialization into JSON significantly faster since there are no class names or property metadata to include in the payload.

However, raw SQL via DB::select bypasses Laravel's model event system and query scopes. If you later decide that converting $result to an array inside a loop is too verbose, consider refactoring your application logic to use Eloquent models where possible. For instance, you could change the query to return a collection of models:

$users = User::all(); // Returns a Collection of User model instances
$usersArray = $users->toArray(); // Returns an array of associative arrays

This shift allows you to leverage relationships, casts, and accessors without writing manual conversion logic. While DB::select offers flexibility for complex aggregations or database-agnostic queries, remember that it returns raw data devoid of model context. If your codebase relies heavily on accessing $user->email and expecting strict types, the manual conversion step in Laravel 5.1 is a necessary workaround to bridge the gap between the database driver's default behavior and your application's expectations.

Our network