Convert laravel object to array

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Efficiently Convert Laravel Objects to Arrays for Enhanced Usability Introduction: Laravel is a powerful PHP framework that simplifies web application development by providing numerous tools and features. One of these features is the ability to store data in various formats, including objects and arrays. At times, you may need to convert object instances into regular arrays for easier manipulation or interoperability with other libraries or systems. In this blog post, we'll explore different approaches to convert Laravel objects to arrays while maintaining the necessary functionality. 1. Understanding Objects and Arrays in Laravel: In PHP, an object is a special type of variable that contains data (properties) and functions (methods). It has its own memory space where it stores all relevant information. In contrast, an array is a collection of multiple values stored in a single variable, allowing us to access specific elements using indexes or keys. Now you know the differences between objects and arrays and how they're used in Laravel. Let's tackle the issue of converting object instances to arrays. 2. Converting Objects to Arrays with foreach Loop: The most basic way to convert an object into an array is by using a simple for-each loop. Here's how it can be done:
$objects = \Illuminate\Support\Facades\DB::connection('mysql')->table('users')->get();

$arrayOfObjects = [];
foreach ($objects as $object) {
    $arrayOfObjects[] = (array)$object;
}
In this code snippet, we first fetch the users from the database and store them in a variable named $objects. We then initialize an empty array called $arrayOfObjects and loop through all the elements of $objects. Inside the loop, we convert each object to an array using (array) cast operator. The result is now stored in the $arrayOfObjects array, with only the data fields remaining. 3. Converting Objects to Arrays Using get_object_vars(): Another method to convert Laravel objects to arrays is by using PHP's built-in function get_object_vars(). This function returns an associative array containing all public properties and methods of the specified object. Here's how you can use it:
$objects = \Illuminate\Support\Facades\DB::connection('mysql')->table('users')->get();

function convertToArray($object) {
    return get_object_vars((array)$object);
}

$arrayOfObjects = array_map("convertToArray", $objects);
In this code, we first fetch the users from the database and store them in a variable named $objects. We then create a custom function called convertToArray() that accepts an object as input and returns a new associative array containing all its properties. Finally, we use PHP's array_map function to apply our custom function on each element of the $objects array, resulting in a new multi-dimensional array with only data fields. 4. Using Laravel Collections: Laravel provides a powerful tool known as Collections that can be utilized for converting objects to arrays. Here's how you can do it:
$objects = \Illuminate\Support\Facades\DB::connection('mysql')->table('users')->get();

use Illuminate\Support\Collection;
$collectionOfObjects = Collection::make($objects);
$flattenedArray = $collectionOfObjects->pluck('ID');
In this code, we first fetch the users from the database and store them in a variable named $objects. We then use collections to create a new Collection instance from the objects array. Lastly, we call the pluck() method on the collection to only select specific properties (in this case, ID) as an array. Conclusion: In conclusion, there are multiple approaches to convert Laravel object instances to arrays. Depending on your requirements and the complexity of the data structure, you can choose one of the methods mentioned above. Regardless of which method you select, always keep in mind that converting to an array may impact the functionality or relationship between objects. For example, if your object has nested arrays within it, flattening them might cause loss of information. Therefore, always test and understand the implications before making any changes to your data structure.