如何在 Laravel 中的通知中获取数据库中的令牌值
How to get the token value in the DB for notifications in Laravel
我正在尝试从 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());
ConfirmUserNotification
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.');
}
Table/Schema
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();
});
}
试试这个。创建一个集合,您可以在其中存储所有新创建的 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));
ConfirmUserNotification
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.');
}
希望这个想法能有所帮助。祝你好运。
我正在尝试从 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());
ConfirmUserNotification
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.');
}
Table/Schema
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();
});
}
试试这个。创建一个集合,您可以在其中存储所有新创建的 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));
ConfirmUserNotification
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.');
}
希望这个想法能有所帮助。祝你好运。