DB query builder toArray() laravel 4

前端 未结 7 1373
野性不改
野性不改 2021-02-01 13:32

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



        
7条回答
  •  误落风尘
    2021-02-01 14:03

    Please note, the option presented below is apparently no longer supported as of Laravel 5.4 (thanks @Alex).

    In Laravel 5.3 and below, there is a method to set the fetch mode for select queries.

    In this case, it might be more efficient to do:

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

    That way, you won't waste time creating objects and then converting them back into arrays.

提交回复
热议问题