How to Convert Object to Array in Laravel?
In this post we will give you information about How to Convert Object to Array in Laravel?. Hear we will give you detail about How to Convert Object to Array in Laravel?And how to use it also give you demo for it if it is necessary.
If you require to convert object data from db into array then you can do it using DB facade and Model Eloquent. Sometimes we need to give array data only so we must get array data from db. I have two examples so it can helps you.
In first example, If you use Model Eloquent for get data from database then you can do it using toArray(). toArray() will help to convert object into array data. So let’s see bellow example and check it.
Example 1:
$users = User::get()->toArray();
print_r($users);
In this example, if you use DB facade for getting data then it’s different from above because when i was try to convert object into array then i can’t do using direct toArray() but i found solution how to do it. You can check bellow example.
Example 2:
DB::setFetchMode(PDO::FETCH_ASSOC);
$users = DB::table("users")->get();
print_r($users);
Hope this code and post will helped you for implement How to Convert Object to Array in Laravel?. 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