gpt4 book ai didi

php - Laravel 5.6 使用 Eloquent ORM 将所有行作为值数组获取

转载 作者:行者123 更新时间:2023-11-29 05:04:21 25 4
gpt4 key购买 nike

我有一个返回集合的 Eloquent 查询:

$items = Item::get(['id', 'name']);

当我转换为 JSON 时,我得到了这个:

[
{id: 1, name: "some name"},
{id: 2, name: "some other name"}
]

我想要这样的结果:

[
[1, "some name"],
[2, "some other name"]
]

我如何使用 Laravel 5.6 和 Eloquent ORM 实现这一点?

最佳答案

可以使用toArray方法

$items = Item::get(['id', 'name'])->map(function($item) {
return array_values($item->toArray());
});

你的 $item 变量会是这样的:

Illuminate\Support\Collection {
all: [
[
1,
"Peter",
],
[
2,
"Juan",
],
];

和 json 表示:

[[1, "Peter"], [2, "Juan"]]

关于php - Laravel 5.6 使用 Eloquent ORM 将所有行作为值数组获取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51542674/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com