PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, February 16, 2022

[FIXED] Return values only (no keys/associative array) in Laravel

 February 16, 2022     arrays, eloquent, laravel, php, query-builder     No comments   

Issue

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?


Solution

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);


Answered By - totymedli
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing