Situation
I have the following code to get all data as an array:
Data::select('id', 'text')->get()->toArray();
This will return the data in the following format:
array:1 [
0 => array:2 [
"id" => "1"
"text" => "Stack"
]
1 => array:2 [
"id" => "2"
"text" => "Overflow"
]
]
But I want only the values as a regular array (no keys/associative array) so the array is not converted to an object when I convert it to JSON:
array:1 [
0 => array:2 [
0 => "1"
1 => "Stack"
]
1 => array:2 [
0 => "2"
1 => "Overflow"
]
]
Inadequate solutions
I know I can convert this with a loop and use array_values()
, but the former is not a one liner while the second works only for one level and not for arrays of arrays.
Also I’m looking for a way to “configure” Eloquent/Query Builder, and not a method to convert the once returned results.
Questions
Is there a setting or a way I can do this with Eloquent/Query Builder?
Advertisement
Answer
TL;DR
Just tell PDO to work this way:
DB::connection()->setFetchMode(PDO::FETCH_NUM);
Data::select('id', 'text')->get()->toArray();
DB::connection()->setFetchMode(PDO::FETCH_OBJ;);
Don’t forget to set back the default value or whatever was your previous setting. Also you will need to use these facades:
use DB;
use PDO;
In details (behind the scenes)
This is controlled via the underlying PDO itself, that can by controlled via fetch_style
. The constant we need is this one:
PDO::FETCH_NUM: returns an array indexed by column number as returned in your result set, starting at column 0
Now we just have to pass this in a Laravel way. This constant is passed to PDO in the Illuminate/Database/Connection.php
file in the select()
function’s last line with a help of a getter:
public function select($query, $bindings = [], $useReadPdo = true)
{
return $this->run($query, $bindings, function ($me, $query, $bindings) use ($useReadPdo) {
if ($me->pretending()) {
return [];
}
// For select statements, we'll simply execute the query and return an array
// of the database result set. Each element in the array will be a single
// row from the database table, and will either be an array or objects.
$statement = $this->getPdoForSelect($useReadPdo)->prepare($query);
$statement->execute($me->prepareBindings($bindings));
return $statement->fetchAll($me->getFetchMode());
});
}
Off course there is a public setter too: setFetchMode()
, so we just have to receive the connector and we can set it. According to the documentation:
When using multiple connections, you may access each connection via the
connection
method on theDB
facade.
So we have everything to do this:
DB::connection()->setFetchMode(PDO::FETCH_NUM);