起初听起来很简单,但我找不到解决办法!
这是我的收藏:
Collection {#433
#items: array:432 [
0 => array:5 [
"word_id" => 12218
"name" => "ordered"
"rank" => 12217
"is_real" => 1
"id" => 1
]
1 => array:5 [
"word_id" => 12097
"name" => "one-dimensional"
"rank" => 12096
"is_real" => 1
"id" => 2
]
2 => array:5 [
"word_id" => 19679
"name" => "watery"
"rank" => 19678
"is_real" => 1
"id" => 3
]
.
.
.
但我希望它是这样的:
Collection {#433
#items: array:432 [
0 => array:5 [
"name" => "ordered"
"id" => 1
]
1 => array:5 [
"name" => "one-dimensional"
"id" => 2
]
2 => array:5 [
"name" => "watery"
"id" => 3
]
.
.
.
如何通过laravel收集来完成?我是否必须将我的集合更改为数组并自行进行操作?怎么样?
答案 0 :(得分:3)
$collection = $collection->map(function ($item) {
return array_only($item, ['id', 'name']);
});
答案 1 :(得分:0)
或者您也可以使用
$collection = $collection->map(function ($item) {
return $item->only(['id', 'name'])
});
答案 2 :(得分:0)
或者您也可以使用
$collection = $collection ->map(function ($item) {
return collect($item)->forget(['work_id','rank','is_real']);
});