PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, February 8, 2022

[FIXED] I need to be able to avoid this error and not stop the foreach where the error is and continue with the others

 February 08, 2022     laravel, php     No comments   

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
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing