4
votes

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?

2

2 Answers

1
votes

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 the DB facade.

So we have everything to do this:

DB::connection()->setFetchMode(PDO::FETCH_NUM);
0
votes

Method:

DB::connection()->setFetchMode(PDO::FETCH_NUM);
Data::select('id', 'text')->get()->toArray();
DB::connection()->setFetchMode(PDO::FETCH_OBJ;);

Is no longer an option after laravel 5.4

Possible workaround is as https://github.com/awesomedeba10 suggested here: https://github.com/laravel/framework/issues/17557#issuecomment-536879106

You could add .env variable with default value in config.

config/database.php:

fetch_mode => env('DB_FETCHMODE', 'FETCH_OBJ');

.env:

FETCH_MODE=FETCH_OBJ

Then in Illuminate/Database/Connection.php>prepared

add this:

$statement->setFetchMode($config['fetch_mode'] == "FETCH_OBJ" ? 5 : ($config['fetch_mode'] == "FETCH_NUM" ? 3 : 2));

insetad of:

$this->fetchMode

Then you can overwrite your config with:

config()->set('database.connections.mysql.fetch_mode', 'FETCH_NUM');

This keeps fetch mode globally default FETCH_OBJ, yet you can still switch to FETCH_NUM whenever you need.