DB query builder toArray() laravel 4

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

    You can do this using the query builder. Just use SELECT instead of TABLE and GET.

    DB::select('select * from user where name = ?',['Jhon']);
    

    Notes: 1. Multiple question marks are allowed. 2. The second parameter must be an array, even if there is only one parameter. 3. Laravel will automatically clean parameters, so you don't have to.

    Further info here: http://laravel.com/docs/5.0/database#running-queries

    Hmmmmmm, turns out that still returns a standard class for me when I don't use a where clause. I found this helped:

    foreach($results as $result)
    {
    print_r(get_object_vars($result));
    }
    

    However, get_object_vars isn't recursive, so don't use it on $results.

提交回复
热议问题