laravel 集合 transform


集合简介(详细见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}

  }

}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM