使用 Laravel 5.3 的通知功能,我发现通知是以这种方式发送给用户的:
$user->notify(new InvoicePaid($invoice));
我认为$user
是可通知实体。如果我想要向尚未拥有帐户的用户发送通知怎么办?在我的应用中,用户可以邀请他们的朋友加入。我正在尝试使用Laravel的通知功能将邀请码发送给尚未拥有帐户的用户。
当用户邀请某人时,他们的数据将存储在邀请模型中,如下所示:
class Invite extends Model
{
protected $table = 'invites';
protected $fillable = array('name', 'email', 'invite_code', 'expires_at', 'invited_by');
protected $dates = ['expires_at'];
}
我曾以为我可以使用notify方法像这样向Invite模型发送通知:
$invite = Invite::find($inviteId);
$invite->notify(new InvitationNotification(ucfirst($invite->name), $invite->invite_code, $invite->expires_at));
但是上面的方法不起作用。我收到了错误消息:
Call to undefined method Illuminate\Database\Query\Builder::notify()
因此,我的问题是:
我是否能够仅向用户模型发送通知?
有没有办法向尚未拥有帐户的新用户发送通知?
在这些情况下,我唯一的选择是使用 Laravel 的 Mailable 类而不是 Notification 吗?
$invitation->user->notify(new WhateverNotification)
。 - chrisbjr