集合简介(详细见laravel 文档):
transform() {#collection-method}#
注意:与大多数集合方法不同, transform 会修改集合本身。如果你想创建新集合,可以使用 map 方法。
$collection = collect([1, 2, 3, 4, 5]);
$collection->transform(function ($item, $key) {
return $item * 2;
});
$collection->all();
// [2, 4, 6, 8, 10]
实例:
$prod = Providers::where('status','normal')->get(); $providers->transform(function ($item, $key) { $item->product = ProductB2b::getProduct($item->id); return $item; });
之前的数据结构:
{
{
"id":19
},
{
"id":18
}
}
transform之后:
{
{
"id":19,
"product":{collection}
},
{
"id":18,
"product":{collection}
}
}