重命名laravel集合“数据"钥匙 [英] Rename laravel collection "data" key
本文介绍了重命名laravel集合“数据"钥匙的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以通过这种方式重命名laravel收集结果中的数据密钥吗?此刻
Is there way i can rename the data key in laravel collection results? At the moment this
DB::table('contracts AS C')->paginate(1)
返回
{
"current_page": 1,
"data": [
{
"id": 191,
"buyer": "John",
"provider": "Jane"
}
],
"from": 1,
"last_page": 1,
"next_page_url": "",
"path": "",
"per_page": 1,
"prev_page_url": null,
"to": 1,
"total": 1
}
我想要的是
{
"current_page": 1,
"parties": [
{
"id": 191,
"buyer": "John",
"provider": "Jane"
}
],
"from": 1,
"last_page": 1,
"next_page_url": "",
"path": "",
"per_page": 1,
"prev_page_url": null,
"to": 1,
"total": 1
}
将数据密钥更改为参与方的挑战
The challenge here changing the data key to parties
推荐答案
您可以使用 keyBy()
收集方法.
You could use the keyBy()
method of collection.
$unfiltered = DB::table('contracts AS C')->paginate(1);
$filtered = $unfiltered->keyBy(function ($value, $key) {
if ($key == 'data') {
return 'parties';
} else {
return $key;
}
});
return $filtered;
这篇关于重命名laravel集合“数据"钥匙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文