如何将电子邮件格式化为数组 - Laravel 5.2

How to format email to an array - Laravel 5.2

我需要向许多用户发送电子邮件通知。我循环获取用户的电子邮件,如下所示:

$userEmails = $favoriteGuides->map(function( $relation ){
      return $relation->users->email;
});

结果是这样的:

Collection {#400 ▼
  #items: array:3 [▼
    0 => "fake@hotmail.com"
    1 => "fake1@hotmail.com"
    2 => "fake3@aol.com"
.... and so on
  ]
}

我需要对其进行格式化,以便将其放入一个数组中,这样我就可以将它发送到 Mail 函数中。它需要这样格式化:

$emails = ["fake@hotmail.com", "fake2@hotmail.com", "and so on..."];

我要把它格式化成我想要的结果吗?

我已经尝试过 - 内爆 - 但没有得到我想要的结果。

您可以使用 toArray() 方法:

The toArray method converts the collection into a plain PHP array. If the collection's values are Eloquent models, the models will also be converted to arrays:

因此,您的代码将如下所示:

$userEmails = $favoriteGuides->map(function( $relation ){
      return $relation->users->email->toArray();
});

使用toArray()方法:

$userEmails->toArray();