gpt4 book ai didi

laravel - 在 laravel 中将数据传递给通知

转载 作者:行者123 更新时间:2023-12-05 07:14:07 26 4
gpt4 key购买 nike

一段时间以来,一直对如何将两个变量传递到通知中并让该通知包含一个包含带有 token 的路由的操作感到困惑。用例是我的应用程序允许用户(此处称为顾问)向其他用户授予访问权限。如果添加的用户尚未注册,他们的帐户将被创建,他们将收到一封电子邮件,其中包含用于完成注册的 token 。

我的 AddAdvisorsController 中有以下内容:

 else {


$newadvisor = Advisor::create($data);
$newadvisor->save();

$newadvisorID = $newadvisor->id;
$newAdvisorEmail = $newadvisor->email;

//create a token
$token = Str::random(60);
//email advisor and pass $token variable to notification
$newadvisor->notify(new NewAdvisorNotification($token, $newAdvisorEmail));

我的 NewAdvisorNotification 中有以下内容:

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class NewAdvisorNotification extends Notification
{
use Queueable;

public $token;



/**
* Create a new notification instance.
*
* @return void
*/
public function __construct($token, $newAdvisorEmail)
{
//
$this->token = $token;
$this->newAdvisorEmail = $newAdvisorEmail;


}

/**
* 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)
->line('You are receiving this email because we received a new account request for your email.')
->action('Notification Action', route('new.advisor', [$this->token, $this->newAdvisorEmail]))
// ->action('Notification Action', route('advisor/new-account/{{ $token }}/{newAdvisorEmail}'))
->line('If you did not request a password reset, no further action is required.');
}

/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
//
];
}

新用户已在数据库中正确创建,但我收到一条错误消息:

[路由:update.advisor] [URI:advisor/new-account/{token}/{newAdvisorEmail}] 缺少必需的参数。 ( View :C:\xampp\htdocs\HealthHub\resources\views\auth\new-accounts\advisor-new-account.blade.php)

我有以下路线:

    Route::get('/advisor/new-account/{token}/{newAdvisorEmail}', 'NewAdvisorController@showNewAccountForm')->name('new.advisor');
Route::post('/advisor/new-account/{token}/{newAdvisorEmail}', 'NewAdvisorController@updateNewAccount')->name('update.advisor');

});

我认为我的代码在这一行有错误:

                ->action('Notification Action', route('new.advisor', [$this->token, $this->newAdvisorEmail]))

但是我不知道怎么解决

最佳答案

您的错误清楚地表明您缺少路线参数。路由参数的指定方式是通过名称,所以声明一个以路由参数名称为键的关联数组。

 ->action('Notification Action', route('new.advisor', ['token' => $this->token, 'newAdvisorEmail' => $this->newAdvisorEmail,]))

关于laravel - 在 laravel 中将数据传递给通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59976640/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com