Laravel 5.3 向没有帐户的用户发送通知
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40780098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5.3 Send Notification to Users without an account
提问by Neel
With the Laravel 5.3 Notification feature, I see that the notifications are sent to users like this:
使用 Laravel 5.3 通知功能,我看到通知是这样发送给用户的:
$user->notify(new InvoicePaid($invoice));
Where I believe $user
is the notifiable entity. What if I want to send notifications to users who doesn't have an account yet? In my app, users send invitation to their friends to join. I am trying to use Laravel's Notification to send the invite code to users who don't have an account yet.
我认为$user
是应通报实体。如果我想向还没有帐户的用户发送通知怎么办?在我的应用中,用户向他们的朋友发送加入邀请。我正在尝试使用 Laravel 的通知将邀请码发送给还没有帐户的用户。
When users invite someone, their data is stored in Invite Model like this:
当用户邀请某人时,他们的数据存储在邀请模型中,如下所示:
class Invite extends Model
{
protected $table = 'invites';
protected $fillable = array('name', 'email', 'invite_code', 'expires_at', 'invited_by');
protected $dates = ['expires_at'];
}
I thought I can use notify to send notifications to the Invite model like this:
我想我可以使用 notify 向 Invite 模型发送通知,如下所示:
$invite = Invite::find($inviteId);
$invite->notify(new InvitationNotification(ucfirst($invite->name), $invite->invite_code, $invite->expires_at));
But the above doesnt work. I get the error:
但以上不起作用。我收到错误:
Call to undefined method Illuminate\Database\Query\Builder::notify()
So my question is:
所以我的问题是:
Am I able to send Notification only to User Model?
Is there a way to send Notifications to new users who doesnt have an account yet?
Is my only choice is to use Laravel's Mailable class instead of Notification in these cases?
我可以只向用户模型发送通知吗?
有没有办法向还没有帐户的新用户发送通知?
在这些情况下,我唯一的选择是使用 Laravel 的 Mailable 类而不是 Notification 吗?
采纳答案by Andreas Elia
I found your post looking for the same thing but answered it myself.
我发现你的帖子在寻找同样的东西,但我自己回答了。
You need to use Notifiable;
on the Invite
model.
你需要use Notifiable;
在Invite
模型上。
You then import use Illuminate\Notifications\Notifiable;
and should be able to use ->notify
on the Invite
model.
然后您导入use Illuminate\Notifications\Notifiable;
并且应该能够->notify
在Invite
模型上使用。
$invite = Invite::create([
'name' => $request->get('name'),
'email' => $request->get('email'),
'token' => str_random(60),
]);
$invite->notify(new UserInvite());
That is how I handle sending the notification :)
这就是我处理发送通知的方式:)
You can then pass through $invite
and use that within the notification template.
然后,您可以通过$invite
并在通知模板中使用它。
回答by thephper
Some alternative solution could be to just new up a user model and set the email attribute without saving it. The instance can now be notified.
一些替代解决方案可能是新建一个用户模型并设置电子邮件属性而不保存它。现在可以通知实例。
I don't see this as the correct approach for this particular case, but for the questions written and since this thread provides some different ways to notify a non-existing user I thought I would write it here.
我不认为这是针对这种特殊情况的正确方法,但对于所写的问题,并且由于该线程提供了一些不同的方法来通知不存在的用户,因此我认为我会在这里写它。
$invitedUser = new User;
$invitedUser->email = '[email protected]';
$invitedUser->notify(new InvitationNotification());
Beware, that this solution may cause problems when queueing notification (because it doesn't reference a specific user id).
请注意,此解决方案在排队通知时可能会导致问题(因为它不引用特定的用户 ID)。
In Laravel >=5.5it is possible to make an "on-demand notification", which covers this exact case, where you want to notify a non-existing user.
在 Laravel >=5.5 中,可以进行“按需通知”,它涵盖了您想要通知不存在的用户的确切情况。
Example from the docs:
文档中的示例:
Notification::route('mail', '[email protected]')
->route('nexmo', '5555555555')
->notify(new InvoicePaid($invoice));
This will send the notification to the email address, and that approach can be queued etc.
这会将通知发送到电子邮件地址,并且该方法可以排队等。
The docs: https://laravel.com/docs/5.5/notifications#on-demand-notifications
文档:https: //laravel.com/docs/5.5/notifications#on-demand-notifications
回答by Martin Bean
Notifications are meant to be sent to a notifiableobject. You don't “notify” an invitation.
通知旨在发送到可通知对象。您不会“通知”邀请。
If all you're wanting to do is email someone that they've been invited to use an application, then just send an email to that email address. Notifications aren't appropriate in this instance.
如果您想要做的只是向某人发送电子邮件,告知他们已被邀请使用某个应用程序,那么只需向该电子邮件地址发送一封电子邮件即可。在这种情况下,通知不合适。
回答by Pendragon
I just figured out a simpler way to this. First you have to override the 'register' function in your RegisterController class. In order to do that:
我只是想出了一个更简单的方法。首先,您必须覆盖 RegisterController 类中的“注册”功能。为了做到这一点:
use Illuminate\Http\Request;
use Illuminate\Auth\Events\Registered;
public function register(Request $request)
{
}
Then add these lines in your register function;
然后在你的寄存器函数中添加这些行;
$user = $this->create($request->all());
//To create and save the user$user->notify(new NewRegistrationNotification($request));
//To call the specified notification classreturn redirect()->to('/')->with('msg', 'Thanks for registering! Check your inbox for the activation link');
$user = $this->create($request->all());
//创建并保存用户$user->notify(new NewRegistrationNotification($request));
//调用指定的通知类return redirect()->to('/')->with('msg', 'Thanks for registering! Check your inbox for the activation link');
Next, configure your Notification class to handle the message send. I did something like this in my constructor;
接下来,配置您的 Notification 类来处理消息发送。我在我的构造函数中做了这样的事情;
protected $data;
public function __construct(Request $request)
{
$this->data = $request;
}
I hope this helps somebody.
我希望这对某人有帮助。