DB query builder toArray() laravel 4

Viewed 137913

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

Tested in Laravel 5.6

The fastest and simple way is just to use json_decode (No need to use json_encode), example:

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

echo json_decode($records,true);

Easiest way is using laravel toArray function itself:

    $result = array_map(function ($value) {
        return $value instanceof Arrayable ? $value->toArray() : $value;
    }, $result);

try this one

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

just remove the "=" sign . . . .because you are trying to array just the name 'jhon' . . . . . . . .I hope it's help you . .

Related