集合簡介(詳細見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}
}
}