DB query builder toArray() laravel 4

Fabrizio Fenoglio picture Fabrizio Fenoglio · Dec 25, 2013 · Viewed 96.8k times · Source

I'm trying to convert a query to an array with the method toArray() but it doesn't work for the query builder. Any ideas for convert it?

Example

DB::table('user')->where('name',=,'Jhon')->get()->toArray();

Answer

Somwang Souksavatd picture Somwang Souksavatd · Jun 15, 2015

If you prefer to use Query Builder instead of Eloquent here is the solutions

$result = DB::table('user')->where('name',=,'Jhon')->get();

First Solution

$array = (array) $result;

Second Solution

$array = get_object_vars($result);

Third Solution

$array = json_decode(json_encode($result), true);

hope it may help