Reputation: 41350
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...
)
Upvotes: 42
Views: 131886
Reputation: 993
Good to pick through the answers here to understand the reasons. Here's my version that I settled on:
$users = collect($users) // turn the top level array into a collection
->map(fn($user) => (array) $user) // cast each object to an array
->toArray(); // convert top level collection back to an array
You had an array of objects. Now you will have an array of arrays. Note that this doesn't do a recursive conversion of the internal array, but since it's only one level deep you're fine.
Upvotes: 0
Reputation: 129
One tricky and simple way :
//Got array in stdClass converting to JSON
$temp1= json_encode($result);
//JSON to Array
$temp2= json_decode($temp1,1);`
May be it will help you
Upvotes: -1
Reputation: 1431
You can make a collection in order to use toArray()
, very simple approach:
$result = DB::table('users')->get();
$data = collect($result)->map(function($x){ return (array) $x; })->toArray();
Upvotes: 1
Reputation: 41350
Here is another approach that is worth to be mentioned:
$resultArray = json_decode(json_encode($result), true);
Upvotes: 13
Reputation: 2519
You can use this one. It's worked for me.
$query = DB::table('user')->select(['id','name'])->get();
$userlist = $query->toArray();
Upvotes: -2
Reputation: 885
you can try this
DB::table('commune')->where('Commune','<>', 'ND')->orderBy('Commune')->get()->pluck('Commune');
Upvotes: 4
Reputation: 4235
Easiest way is using laravel toArray function:
public function toArray()
{
return array_map(function ($value) {
return $value instanceof Arrayable ? $value->toArray() : $value;
}, $this->items);
}
Upvotes: -4
Reputation: 191
For laravel/lumen 5.4 (that uses illuminate/database 5.4):
$pdo = DB::getPdo();
$statement = $pdo->prepare($yourQuery);
$statement->setFetchMode(\PDO::FETCH_ASSOC);
$statement->execute();
$results = $statement->fetchAll();
Or listen to Illuminate\Database\Events\StatementPrepared event: Laravel 5.4 - How to set PDO Fetch Mode?
Upvotes: 4
Reputation: 111839
The best solutions looking at performance would be changing method how data is grabbed from database:
// get original model
$fetchMode = DB::getFetchMode();
// set mode to custom
DB::setFetchMode(\PDO::FETCH_ASSOC);
// get data
$result = DB::select($sql);
// restore mode the original
DB::setFetchMode($fetchMode);
Obviously if you want to get all the data as array, it will be enough to change in config/database.php
from:
'fetch' => PDO::FETCH_CLASS,
into
'fetch' => PDO::FETCH_ASSOC,
and then running only:
$result = DB::select($sql);
will return you multidimensional array instead of array of objects
Upvotes: 34
Reputation: 4397
You can do this way..
At the top
use DB;
use PDO;
--------------------
DB::setFetchMode(PDO::FETCH_ASSOC); // Set the fetch mode as array
$result = DB::select('select * from users');
For example, now you can get result like this way.
$first_email = $result[0]['email'];
Now you can change to default fetch mode.
DB::setFetchMode(PDO::FETCH_CLASS);
Upvotes: -1
Reputation: 163768
toArray()
method converts collection to an array. Models are converted to arrays too:
The
toArray
method converts the collection into a plain PHP array. If the collection's values areEloquent
models, the models will also be converted to arrays
So, I guess you're doing something wrong. If you want more help, please update you question with all related code.
Also, only Eloquent collections (models) have toArray()
method.
To use this method, use Eloquent:
$result = \App\User::all()->toArray();
Upvotes: 11
Reputation: 4795
Try this
$result = DB::select($sql);
$arr = [];
foreach($result as $row)
{
$arr[] = (array) $row;
}
Upvotes: -1
Reputation: 728
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);
Upvotes: 59