Laravel - How to convert Stdclass object to Array

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company
# Laravel - How to Convert StdClass Object Result from Database Query to an Array As developers working with Laravel, one of the most common initial hurdles is understanding how the raw results returned from the database map into PHP structures. When you execute a query using methods like `get()` on the Query Builder or Eloquent models, you often receive a `Illuminate\Support\Collection` of objects, which might sometimes appear as `stdClass` instances when dealing with raw results. The question is: **How can I convert this result into a simple, usable PHP array the most simple way?** This post will walk you through the most efficient and idiomatic ways to handle this conversion in Laravel, moving from basic retrieval to complex data manipulation. --- ## Understanding the Result Type When you execute `$data = DB::table('users')->get();`, Laravel returns an instance of `Illuminate\Support\Collection`. Each item within that collection is an object (often a `stdClass` if not using Eloquent models). To get a simple, flat PHP array from this structure, we need to iterate over the collection and extract the properties. While you *could* loop manually, Laravel provides much cleaner, more expressive methods designed exactly for this purpose. ## Method 1: The Simplest Way – Using `toArray()` The absolute simplest method to convert an entire `Collection` (or a `stdClass` object if that is the direct result) into a standard PHP array is by calling the built-in `toArray()` method. This method recursively converts all elements of the collection into a simple associative array. If your query returns a Collection, this is the fastest approach: ```php use Illuminate\Support\Facades\DB; // Retrieve results $data = DB::table('users')->get(); // Convert the Collection directly to an Array $usersArray = $data->toArray(); // $usersArray is now a standard PHP array, ready for use. ``` This method is highly recommended because it leverages Laravel's collection functionality and avoids manual looping, keeping your code clean and readable—a principle central to good development practices found on sites like [laravelcompany.com](https://laravelcompany.com). ## Method 2: Extracting Specific Columns using `pluck()` If you don't need the entire object structure, but only a specific set of column values (e.g., just the user IDs or usernames), using the `pluck()` method is significantly more efficient than fetching the whole object and then converting it to an array. `pluck()` returns a new collection containing only the specified value from each item. You can then easily convert this result to an array: ```php use Illuminate\Support\Facades\DB; // Get only the 'id' column into an array $userIds = DB::table('users')->pluck('id'); // $userIds is now a Collection of IDs, which can be cast to an array if needed. $idsArray = $userIds->toArray(); // If you want an array of usernames: $usernames = DB::table('users')->pluck('name'); $namesArray = $usernames->toArray(); ``` This approach is much more performant when your goal is data extraction rather than object serialization. ## Method 3: Transforming Data using `map()` If you need to transform the data—for instance, renaming keys or calculating a new field—the `map()` method within the Collection provides the perfect tool. You can iterate over every item and return a modified version of it. ```php use Illuminate\Support\Facades\DB; $data = DB::table('users')->get(); // Transform the collection: create an array where keys are 'name' and values are objects $transformedData = $data->map(function ($user) { return [ 'name' => $user->name, 'full_name' => strtoupper($user->name), 'id' => $user->id, ]; }); // Convert the resulting collection of arrays into a final array structure $finalArray = $transformedData->toArray(); /* The structure of $finalArray will look something like: [ ['name' => 'Alice', 'full_name' => 'ALICE', 'id' => 1], ['name' => 'Bob', 'full_name' => 'BOB', 'id' => 2], ] */ ``` ## Conclusion For converting database results into usable PHP arrays in Laravel, always favor the built-in Collection methods over manual looping. 1. Use **`toArray()`** when you need the entire set of objects converted to an array. 2. Use **`pluck('column_name')`** when you only need a single column's values (most performant). 3. Use **`map()`** when you need to restructure or transform the data before final serialization. By adopting these patterns, you ensure your Laravel applications remain clean, efficient, and maintainable, adhering to the best practices promoted by the community at [laravelcompany.com](https://laravelcompany.com).