[英]Spatie Laravel 9: send email notification to Users with roles
我在发送电子邮件和模板方面一切正常。 现在我想将 static 替换to: email@test.com
为具有特定角色的用户 email。
我写了这段代码:
public function envelope()
{
return new Envelope(
from: 'amushref@hsse.co',
to: [
User::with("roles")->whereHas("roles", function($q) {
$q->whereIn("id", [
1, // Super Admin
6, // Admin
2, // Security Supervisor
5, // Security Manager
]);
})->get('email')
],
subject: 'New Incident: ' . str_pad($this->record->ir_number, 4, '0', STR_PAD_LEFT) .
' - ' .
$this->record->caseTypeRelationship->name .
' - ' . $this->record->locationRelationship->name,
);
}
我已经做到to:
作为一个包含所提供角色 ( id
) 的电子邮件的数组。 我收到一条错误消息,指出地址不正确/不存在。 获取所选角色用户电子邮件的正确方法是什么?
首先,如果不需要,则不需要包含roles
。 它引入了一个您应该避免的额外查询。
其次,您的->get('email')
返回一个仅包含email
字段的用户Collection
。 您可能希望将其转换为数组以将其传递给Envelope
。
第三,你已经包装了你的->get('email')
的 output 它本身已经是一个集合在它自己的数组中,使你的 email 坐得太深了。
这样的事情应该可以解决问题:
to: User::whereHas("roles", function($q) {
$q->whereIn("id", [1, 6, 2, 5]);
})->pluck('email')->toArray(),
您可以获得包含以下内容的电子邮件列表并将其分配给数组:
$emails = User::role(['Super Admin', 'Admin', 'Security Supervisor', 'Security Manager'])
->pluck('email')->toArray();
然后将您的更改to: $emails
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.