0

我正在尝试从 ConfirmUserToken 获取令牌并将其附加到通知中的 URL。我正在尝试在 laravel 中创建自定义验证。当我使用 $notifiable->token 时,它没有给我任何结果。如何访问令牌?

注册控制器

foreach ($users as $u) {
    $cutoken = ConfirmUserToken::create([
        'user_id' => $u->id,
        'concerned_user' => $user->id,
        'token' => Hash::make(now()),
    ]);
    $cutoken->user()->associate($u);
}

Notification::send($users, new ConfirmUser());

确认用户通知

public function toMail($notifiable)
{
    $confirmUrl = url("confirmUser/{$notifiable->id}/{$notifiable->token}");

    return (new MailMessage)
        ->subject('Confirm User')
        ->greeting("Dear {$notifiable->name},")
        ->line('Please click the button below to confirm that the concerned user is your staff')
        ->action('Confirm User', $confirmUrl)
        ->line('If you did not know the concerend staff, no further action is required.');
}

表/架构

public function up()
{
    Schema::create('confirm_user_tokens', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->unsignedInteger('user_id')->default(0);
        $table->unsignedInteger('concerned_user')->default(0);
        $table->string('token')->default(0);
        $table->string('status')->default('0');
        $table->timestamps();
    });
}
4

1 回答 1

2

尝试这个。创建一个集合,您可以在其中存储所有新创建的,ConfirmUserToken然后在您的ConfirmUserNotification

像这样的东西。。

注册控制器

$usersToken = collect(); // Create collection

foreach ($users as $u) {
    $cutoken = ConfirmUserToken::create([
        'user_id' => $u->id,
        'concerned_user' => $user->id,
        'token' => Hash::make(now()),
    ]);
    $cutoken->user()->associate($u);

    $usersToken->add($cutoken); // Store new ConfirmUserToken
}

 // pass the $usersToken object
\Notification::send($users, new ConfirmUser($usersToken));

确认用户通知

public $usersToken; 

public function __construct($usersToken)
{
   $this->usersToken = $usersToken;
}


public function toMail($notifiable)
{
    // Find the notifiable user to the object.
    $userToken = $this->usersToken->where('user_id', $notifiable->id)->first();
    // use the token from the first result above. 
    $confirmUrl = url("confirmUser/{$notifiable->id}/{$userToken->token}");

    return (new MailMessage)
        ->subject('Confirm User')
        ->greeting("Dear {$notifiable->name},")
        ->line('Please click the button below to confirm that the concerned user is your staff')
        ->action('Confirm User', $confirmUrl)
        ->line('If you did not know the concerend staff, no further action is required.');
}

希望这个想法能有所帮助。祝你好运。

于 2019-11-19T01:49:55.227 回答