How to convert stdclass object to array in laravel 5.8 – onlinecode
In this post we will give you information about How to convert stdclass object to array in laravel 5.8 – onlinecode. Hear we will give you detail about How to convert stdclass object to array in laravel 5.8 – onlinecodeAnd how to use it also give you demo for it if it is necessary.
Today, We will learn how to convert the stdclass object to the array in laravel 5.8. normally we get data from the database that time the laravel return the collection of or object types formatting data. but sometimes we need to object to array formatting data. so we will learn many ways about how to convert the stdclass object to the array.
You can use the toArray method to convert a stdclass object to array. you can see below example.
$data = DB::table('users')->get()->toArray(); echo "<pre>"; print_r($data); echo "</pre>";
You can also use json_encode to json_decode convert stdclass object to array.
$data = DB::table('users')->get(); $result = json_decode(json_encode($data, true)); echo "<pre>"; print_r($result); echo "</pre>";
You can use setFetchMode to convert a stdclass object to array.
DB::setFetchMode(PDO::FETCH_ASSOC); $data = DB::table('users')->get(); echo "<pre>"; print_r($data); echo "</pre>";
You can change in the database config setting. Doing that setting, All project stdclass object will be converted into the array.
// application/config/database.php 'fetch' => PDO::FETCH_CLASS, // to 'fetch' => PDO::FETCH_ASSOC,
Hope this code and post will helped you for implement How to convert stdclass object to array in laravel 5.8 – onlinecode. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs