Laravel 中仅返回值(无键/关联数组) [英] Return values only (no keys/associative array) in Laravel

查看:87
本文介绍了Laravel 中仅返回值(无键/关联数组)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码将所有数据作为数组获取:

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"
  ]
]

但我只希望将值作为常规数组(无键/关联数组),因此当我将其转换为 JSON 时,该数组不会转换为对象:

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"
  ]
]

不足的解决方案

我知道我可以用循环来转换它并使用 array_values(),但前者不是单行,而后者只适用于一层,不适用于数组数组.

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.

此外,我正在寻找一种配置"Eloquent/Query Builder 的方法,而不是一种转换一次返回结果的方法.

Also I'm looking for a way to "configure" Eloquent/Query Builder, and not a method to convert the once returned results.

是否有设置或方法可以使用 Eloquent/Query Builder 执行此操作?

Is there a setting or a way I can do this with Eloquent/Query Builder?

推荐答案

TL;DR

告诉 PDO 以这种方式工作:

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;

细节(幕后)

这是通过底层 PDO 本身控制的,可以通过 fetch_style 控制.我们需要的常量是这个:

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:返回一个由结果集中返回的列号索引的数组,从第 0 列开始

PDO::FETCH_NUM: returns an array indexed by column number as returned in your result set, starting at column 0

现在我们只需要以 Laravel 的方式传递它.这个常量在select()函数最后一行的Illuminate/Database/Connection.php文件中通过getter传递给PDO:

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

当然还有一个公共设置器:setFetchMode(),所以我们只需要接收连接器,我们就可以设置它.根据文档:

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:

当使用多个连接时,您可以通过 DB 门面上的 connection 方法访问每个连接.

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

这篇关于Laravel 中仅返回值(无键/关联数组)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆