我需要能够避免这个错误,而不是在错误所在的地方停止foreach并继续其他错误



我会更清楚,我需要能够避免这个错误,而不是停止错误所在的foreach并继续其他错误。发送邮件时有没有避免错误的方法?我有一个完整的功能,当电子邮件出错时就会停止,我只想让它继续做这项工作,不管有什么错误

class MailBienvenida extends Notification
{
use Queueable;

protected $nombre;
protected $codigo;


/**
* Create a new notification instance.
*
* @return void
*/
public function __construct($nombre, $codigo)
{
$this->nombre = $nombre;
$this->codigo = $codigo;
}

/**
* Get the notification's delivery channels.
*
* @param  mixed  $notifiable
* @return array
*/
public function via($notifiable)
{
return ['mail'];
}

/**
* Get the mail representation of the notification.
*
* @param  mixed  $notifiable
* @return IlluminateNotificationsMessagesMailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->line('Bienvenido ' . $this->nombre)

}

/**
* Get the array representation of the notification.
*
* @param  mixed  $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}

如果您使用php7及以上版本。您可以抛出异常之类的错误。所有可恢复的错误都是可捕获的。错误和异常都实现了一个名为Throwable的通用接口。

这意味着当出现可丢弃错误时,你可以用try-catch块包围你的呼叫,并简单地继续循环:

foreach ($array as $row) {
try {
$row->executeThatFunction();
} catch (Throwable $t) {
// you may want to add some logging here...
continue;
}
}

最新更新