How to Convert Object to Array in Laravel?

Today now in this post i will show you How to Convert Object to Array in Laravel? If we need to convert a object data from db into an array then we can do it by using DB facade and also Model Eloquent. Sometimes you must need to give an array data only so you must get an array data from the db. Now here I have two examples so i hope it will helps you.

So in the first example, If we are use Model Eloquent for get the data from database then we can do it by using toArray(). toArray() will help us to convert a object into array data.

So let’s see the bellow example and check this.

Example 1:
$users = User::get()->toArray();

Now in the in the second example, if we are use DB facade for getting the data then it’s different from above because when i was try to convert the object into an array then i can’t do by using direct toArray() but i was found a solution for how to do it.

So we can check this in the bellow example.

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

Read Also : Laravel 8 Mobile Number Verification Tutorial

Thanks for read. I hope it help you. For more you can follow us on facebook


About Shahriar Sagor

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Shahriar Sagor →