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();
toArray is a model method of Eloquent, so you need to a Eloquent model, try this: User::where('name', '=', 'Jhon')->get()->toArray(); http://laravel.com/docs/eloquent#collections. Follow this answer to receive notifications.
Laravel's database query builder provides a convenient, fluent interface to creating and running database queries. It can be used to perform most database operations in your application and works perfectly with all of Laravel's supported database systems.
While developing in eloquent, the column name can be passed as an argument in order to extract the values. Pluck () also accepts a second argument and if it happens to be an Eloquent collection, it will be another column name. To further strengthen the pluck() function, the wherein() function can be used.
Check if not null: whereNotNullSELECT * FROM users WHERE last_name IS NOT NULL; The equivalent to the IS NOT NULL condition in Laravel Eloquent is the whereNotNull method, which allows you to verify if a specific column's value is not NULL .
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
toArray is a model method of Eloquent, so you need to a Eloquent model, try this:
User::where('name', '=', 'Jhon')->get()->toArray();
http://laravel.com/docs/eloquent#collections
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With