阅读(1581)
赞(7)
Laravel 8 mapInto() {#collection-method}
2021-07-01 14:46:26 更新
mapInto()
方法可以迭代集合,通过将值传递给构造函数来创建给定类的新实例:
class Currency
{
/**
* Create a new currency instance.
*
* @param string $code
* @return void
*/
function __construct(string $code)
{
$this->code = $code;
}
}
$collection = collect(['USD', 'EUR', 'GBP']);
$currencies = $collection->mapInto(Currency::class);
$currencies->all();
// [Currency('USD'), Currency('EUR'), Currency('GBP')]