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.
$users = User::get()->toArray(); print_r($users);
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.
DB::setFetchMode(\PDO::FETCH_ASSOC); $users = DB::table("users")->get(); print_r($users);
Read Also : Laravel 8 Mobile Number Verification Tutorial
Thanks for read. I hope it help you. For more you can follow us on facebook