How to Convert Object to Array in Laravel?

By Hardik Savani | June 2, 2016 | Category : Laravel

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();


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:


$users = DB::table("users")->get();


Hardik Savani
My name is Hardik Savani. I'm a full-stack developer, entrepreneur and owner of Aatman Infotech. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.
Follow Me: Github Twitter
***Do you want me hire for your Project Work? Then Contact US.