Issue
I am going to be clearer, I need to be able to avoid this error and not stop the foreach where the error is and continue with the others. Is there a way to avoid errors when sending mail? I have a complete function that stops when an email is wrong, I just want it to continue doing the job no matter the error
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 \Illuminate\Notifications\Messages\MailMessage
*/
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 [
//
];
}
}
Solution
If your using php7 and above. you can throws errors like exceptions. And all recoverable errors are catchable. And also both errors and exceptions implement a common interface called Throwable.
That means you can surround your call with a try-catch-block and simply continue the loop, when a throwable error occurs:
foreach ($array as $row) {
try {
$row->executeThatFunction();
} catch (Throwable $t) {
// you may want to add some logging here...
continue;
}
}
Answered By - Wesley Chong
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.