DB query builder toArray() laravel 4

前端 未结 7 1420
野性不改
野性不改 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:08

    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

提交回复
热议问题