I have a large SQL statement that I am executing like so:
$result = DB::select($sql);
For example
$result = DB::select('select * from users');
I'd like the result to be an array - but at the moment it returns a structure like so, an array with Objects...
Array ( 0 => stdClass::__set_state(array( 'id' => 1, 'first_name' => 'Pavel', 'created_at' => '2015-02-23 05:46:33', )), 1 => stdClass::__set_state(array( 'id' => 2, 'first_name' => 'Eugene', 'created_at' => '2016-02-23 05:46:34', )), ...etc...
)
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.
$shop = Shop::leftJoin('ratings', 'ratings. shop_id', '=', 'shops.id') ->select('shops. *', 'ratings. rating')->get(); return response($shop);
This allows you to add conditions throughout your code until you actually want to fetch them, and then you would call the get() function. Think of it like this: when you don't exactly know what a query will return then you need to use get() .
After configuring the database, we can retrieve the records using the DB facade with select method. The syntax of select method is as shown in the following table. Run a select statement against the database.
Seems like you need to cast the stdClass Objects as Array so you can have the structure you are looking for
$result = array_map(function ($value) { return (array)$value; }, $result);
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