如何直接获取每个列的值作为数组,而又不使用pllar laravel?

时间:2019-10-11 12:06:19

标签: php laravel eloquent

hi试图将两列的值作为数组获取,像这样

"result": {
    "time": [1, 2, 4, 5, 6],
    "values": [5, 6, 7, 8, 9]
}

我能够使用:

$result['result'] = DB::table(TimeSignal::getTableName())->select([
    'value',
    DB::raw("(time * 0.001) as time")
])
->where('trip_id', $trip->id)
->where('signal_type_id', $timeSignalType->id)
->orderBy('time', 'asc')
->get();

然后:

$timeSignalData = $result['result'];
$result['result']['time'] = $timeSignalData->pluck('time');
$result['result']['values'] = $timeSignalData->pluck('value');

但是pluck函数需要花费大量时间将对象制成数组,这是他们更快地完成此操作的某种方式,或者我们可以直接通过查询来完成它。

1 个答案:

答案 0 :(得分:1)

我想我可以使用在Refactoring to Collections中找到的宏来解决您的问题。

x = arr.array([3, 6, 9, 12])
TypeError: array() argument 1 must be a unicode character, not list

在视觉上是这样的:

Collection::macro('transpose', function () {
    $items = array_map(function (...$items) {
        return $items;
    }, ...$this->values());
    return new static($items);
});

获取集合中的值

[1,2]                 [1,3,5]
[3,4] => transpose => [2,4,6]
[5,6]

这是到目前为止的样子

$queryCollection = DB::table(TimeSignal::getTableName())->select([
    'value',
    DB::raw("(time * 0.001) as time")
])
->where('trip_id', $trip->id)
->where('signal_type_id', $timeSignalType->id)
->orderBy('time', 'asc')
->get();

将每一行映射到一个数组...

# dump($queryCollection)
Illuminate\Support\Collection {
    all: [
        {
            +"value": 5,
            +"time": 1,
        },
        {
            +"value": 6,
            +"time": 2,
        },
        {
            +"value": 7,
            +"time": 4,
        },
        {
            +"value": 8,
            +"time": 5,
        },
        {
            +"value": 9,
            +"time": 6,
        },
        ...
    ],
}

现在看起来像这样

$rows = $queryCollection->map(function($item) {
    return collect($item)->toArray();
}));

现在我们将其移调。

# dump($rows)
Illuminate\Support\Collection {
    all: [
        [
            "value": 5,
            "time": 1,
        ],
        [
            "value": 6,
            "time": 2,
        ],
        [
            "value": 7,
            "time": 4,
        ],
        [
            "value": 8,
            "time": 5,
        ],
        [
            "value": 9,
            "time": 6,
        ],
        ...
    ],
}

哪个给了我们

$rows = $rows->transpose()

最后,我们添加密钥:

# dump($rows)

Illuminate\Support\Collection {
    all: [
        [
            5,
            6,
            7,
            8,
            9
        ],
        [
            1,
            2,
            4,
            5,
            6,
        ],
    ]
}

最后给我们的是

$result = collect(['values', 'time'])->combine($rows)
# dump($result)
Illuminate\Support\Collection {
    all: [
        "values" => [
            5,
            6,
            7,
            8,
            9
        ],
        "time" => [
            1,
            2,
            4,
            5,
            6,
        ],
    ]
}
# dump($result['time'])
[
    1,
    2,
    4,
    5,
    6,
]

较少步骤:

# dump($result['values'])
[
    5,
    6,
    7,
    8,
    9,
]