【发布时间】:2019-11-26 02:35:44
【问题描述】:
我在我的控制器功能中循环访问用户并向每个用户发送电子邮件,但有一个特殊用户有时会出现在不应该接收电子邮件的列表中。
如果是那个用户,我想添加一个if 语句以跳过该循环迭代,但是当我添加return、return null 等时,或者在可邮寄类中的if/else 中什么都没有我得到的构建函数
InvalidArgumentException
Invalid view.
我可以在控制器中添加条件,我相信这会很好,除非我在我的控制器的整个负载中都有这些电子邮件,所以会多次编写条件。如果我能把它放到一个中心位置(而且我知道的只有可邮寄的类),那么我可以写一次。
编辑:已请求循环代码,因此在下面添加,但这只是我的控制器中的许多电子邮件循环之一,所以不是我想添加条件的地方。
$objNotification = new \stdClass();
$objNotification->message_body = "stuff";
$user_ids = DB::select('select user_id from users_to_things where thing_id = ?', [$thing->id]);
foreach($user_ids as $user_id) {
$user = User::find($user_id->user_id);
$objNotification->receiver = $user->name;
Mail::to($user->email)->send(new NotificationEmail($objNotification));
}
这是我的可邮寄类的构建函数(如果可能的话,我想在其中添加条件):
public function build()
{
if($this->notification->receiver == 'the special user') {
return;
} else {
return $this->from('somebody@somewhere.com', 'Sender Name')
->subject('some subject')
->view('emails.notification')
->text('emails.notification_plain');
}
}
【问题讨论】:
-
请告诉我们您正在使用的循环。如果您不向我们展示您的代码,我们将无法帮助您。
-
没问题@Jerodev,我已经添加了这个和可邮寄的构建功能(我想编辑)。
标签: laravel email model-view-controller laravel-5.7