首页 > 解决方案 > Laravel 8:未定义的属性:App\Notifications\ResetPassword::$queue

问题描述

我想为我的通知应用队列,所以我在我的通知中实现了它ResetPassword

class ResetPassword extends Notification implements ShouldQueue

然后我运行php artisan queue:table并迁移它,以便jobs在数据库中成功创建表。

并更改QUEUE_CONNECTIONdatabaseat.env文件并重新运行php artisan serve.

但是当我对此进行测试并单击重置密码链接时,必须将新的表格行添加到jobs表格中,但事实并非如此。

而不是那个,这个错误返回:

ErrorException未定义属性:App\Notifications\ResetPassword::$queue ...\notification\vendor\laravel\framework\src\Illuminate\Notifications\NotificationSender.php:195

那么这里出了什么问题?我该如何解决这个问题?

我真的很感激你们的任何想法或建议......

提前致谢。

更新#1

ResetPassword.php

<?php

namespace App\Notifications;

use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Facades\Lang;

class ResetPassword extends Notification implements ShouldQueue
{
    /**
     * The password reset token.
     *
     * @var string
     */
    public $token;

    /**
     * The callback that should be used to build the mail message.
     *
     * @var \Closure|null
     */
    public static $toMailCallback;

    /**
     * Create a notification instance.
     *
     * @param  string  $token
     * @return void
     */
    public function __construct($token)
    {
        $this->token = $token;
    }

    /**
     * Get the notification's channels.
     *
     * @param  mixed  $notifiable
     * @return array|string
     */
    public function via($notifiable)
    {
        return ['mail'];
    }

    /**
     * Build the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        if (static::$toMailCallback) {
            return call_user_func(static::$toMailCallback, $notifiable, $this->token);
        }

        return (new MailMessage)
            ->subject('subject goes here')
            ->line('This email is sent to you')
            ->action(Lang::get('Reset Password'), url(config('app.url').route('password.reset', ['token' => $this->token, 'email' => $notifiable->getEmailForPasswordReset()], false)))
            ->line(Lang::get('Until the next 60 minutes you can use this link', ['count' => config('auth.passwords.'.config('auth.defaults.passwords').'.expire')]))
            ->line(Lang::get('If you did not request a password reset, no further action is required.'));
    }

    /**
     * Set a callback that should be used when building the notification mail message.
     *
     * @param  \Closure  $callback
     * @return void
     */
    public static function toMailUsing($callback)
    {
        static::$toMailCallback = $callback;
    }
}

标签: phplaravelnotificationsqueuelaravel-8

解决方案


看起来你忘了Queueable Trait在你的notification

use Illuminate\Bus\Queueable;
class ResetPassword extends Notification implements ShouldQueue
{

   use Queueable;

Queueable trait有财产$queue

参考:https ://laravel.com/docs/8.x/notifications#queued-notifications-and-database-transactions


推荐阅读