我对确认电子邮件邀请对象的代码有疑问。我有一个评论系统。访客阅读帖子时,他可以发表评论。当他添加评论时,客人需要确认电子邮件(在评论表上,我有电子邮件字段)。如何正确进行电子邮件确认?当用户注册时,如何在Laravel中使用。
现在,我有了表“ guests”和一个模型来宾,其列为:valueChanges
,name
,email
和email_token
(在用户表中的方式)。>
在模型访客中,我具有功能:
email_verified_at
创建评论后,我调用观察到的函数public function sendEmailNotification($token)
{
$this->notify((new ReviewEmailNotification($token)));
}
并发送电子邮件:
created
我的通知:
public function created(Review $review)
{
Auth::guest() ? $review->sendEmailNotification($review->email_token) : '';
}
我的确认电子邮件功能:
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Support\Facades\Lang;
class ReviewEmailNotification extends Notification
{
use Queueable;
public $token;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct($token)
{
$this->token = $token;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['mail'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->subject(Lang::getFromJson('Verify comment'))
->line(Lang::getFromJson('Please confirm your Email:'))
->action(Lang::getFromJson('Confirm E-Mail'), url(config('app.url') . route('revemail', $this->token)))
->line(Lang::getFromJson('...'));
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}
当电子邮件被验证后,我在来宾表上创建记录。
此解决方案正在工作。但是我如何摆脱public function activateEmail($token = null)
{
$guest = Guest::where('email_token', $token)->first();
if ($review) {
$guest->email_verified_at = now();
$guest->save();
}
return redirect()->route('home');
}
?在用户表中,我只有email_token
,没有列标记。我想用到期日做令牌,在laravel中怎么做。我可以为访客使用默认的laravel电子邮件确认吗?
答案 0 :(得分:0)
简单地修改当前接受$ token的sendEmailNotification,将其更改为电子邮件,并在使用令牌的函数中将其更改为电子邮件,如下所示:
打开此功能 ReviewEmailNotification
modify like this Review::where('email',$email)
想要更多帮助,然后发布 ReviewEmailNotification
的完整代码答案 1 :(得分:0)
最后我明白了您的查询,您需要更改代码。...不要为此使用用户表,因为在每个帖子/文章中都会发布新的评论/评论,因此来宾表会很好,并添加post_id,user_id电子邮件确认,因此您的新功能如下所示:
public function created(Review $review)
{
Auth::guest() ? $review->sendEmailNotification($review-> post_id,$review->user_id,) : '';
}
public function activateEmail($token = null)
{
$guest = Guest::where('post_id', $post_id)->where('user_id', $user_id)->first();
if ($review) {
$guest->email_verified_at = now();
$guest->save();
}
return redirect()->route('home');
}
public function sendEmailNotification($user_id,$post_id)
{
$this->notify((new ReviewEmailNotification($post_id,$user_id )));
}