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