title: Laravel skip sending mail based on conditions
body: <p>I'm looping through users in my controller function and sending an email to each user, but there's a special user who will sometimes be in the list who shouldn't receive emails.</p>
  
  <p>I'd like to put an <code>if</code> statement in to skip that loop iteration if it's that user, but when I add <code>return</code>, <code>return null</code> etc, or just nothing in my <code>if/else</code> in the mailable class build function I get</p>
  
  <pre><code>InvalidArgumentException
  Invalid view.
  </code></pre>
  
  <p>I could add the conditional in the controller and I'm sure that would be fine, except I have these emails in a whole load of my controllers so would be writing the conditional many times. If I can get it into a central location (and the mailable class is the only one I'm aware of) then I can write it once.</p>
  
  <p>EDIT: the loop code has been requested so added below, but this is just one of many email loops in my controllers so NOT where I'd like to add my conditional.</p>
  
  <pre><code>        $objNotification = new \stdClass();
          $objNotification-&gt;message_body = "stuff";
          $user_ids = DB::select('select user_id from users_to_things where thing_id = ?', [$thing-&gt;id]);
          foreach($user_ids as $user_id) {
              $user = User::find($user_id-&gt;user_id);
              $objNotification-&gt;receiver = $user-&gt;name;
              Mail::to($user-&gt;email)-&gt;send(new NotificationEmail($objNotification));
          }
  </code></pre>
  
  <p>Here's my mailable class's build function (where I DO want to add the conditional if at all possible):</p>
  
  <pre><code>public function build()
  {
      if($this-&gt;notification-&gt;receiver == 'the special user') {
          return;
      } else {
          return $this-&gt;from('somebody@somewhere.com', 'Sender Name')
                      -&gt;subject('some subject')
                      -&gt;view('emails.notification')
                      -&gt;text('emails.notification_plain');
      }
  }
  </code></pre>