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();
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();
You can do this using the query builder. Just use SELECT instead of TABLE and GET.
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:
However, get_object_vars isn't recursive, so don't use it on $results.
toArray is a model method of Eloquent, so you need to a Eloquent model, try this:
http://laravel.com/docs/eloquent#collections
And another solution
It good in case when you need only several columns from entity.
If you prefer to use Query Builder instead of Eloquent here is the solutions
First Solution
Second Solution
Third Solution
hope it may help
Easiest way is using laravel toArray function itself:
try this one
just remove the "=" sign . . . .because you are trying to array just the name 'jhon' . . . . . . . .I hope it's help you . .