【发布时间】:2021-03-16 09:28:10
【问题描述】:
我有这样的集合 A...
[
"Gender",
"Name",
"Role",
],
...我有一个来自这样的模型实例的集合 B...
[
App\Models\Staff {
id: 11,
name: "John Doe",
gender: 'Male',
2: "role",
role: App\Models\Role {
id: 1,
name: "Director",
},
},
App\Models\Staff {
id: 22,
name: "Jane Doe",
gender: 'Female',
2: "role",
role: App\Models\Role {
id: 2,
name: "Company Secretary",
},
},
],
我怎样才能将两者结合起来,使我拥有这样的 Collection C...
[
[
"Gender" => 'Male',
"Name" => 'John Doe',
"Role" => 'Director',
],
[
"Gender" => 'Female',
"Name" => 'Jane Doe',
"Role" => 'Company Secretary',
],
]
到目前为止,我已经尝试了一些事情,包括以此为起点。
return $collectionB->map(function ($b) use ($collectionA) {
return $collectionA->map(function ($a) use ($b) {
return [
$a => $b['gender'],
$a => $b['name'],
$a => $b['role']['name'],
];
})->all();
})->all();
【问题讨论】:
标签: php laravel collections