【问题标题】:Laravel 8: Undefined property: App\Notifications\ResetPassword::$queueLaravel 8:未定义的属性:App\Notifications\ResetPassword::$queue
【发布时间】:2021-06-07 06:08:33
【问题描述】:

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

class ResetPassword extends Notification implements ShouldQueue

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

并且还将.env 文件中的QUEUE_CONNECTION 更改为database,然后重新运行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;
    }
}

【问题讨论】:

  • @JohnLobo 我刚刚添加了一个 UPDATE #1

标签: php laravel notifications queue laravel-8


【解决方案1】:

看起来你忘记在notification 中使用Queueable Trait

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-20
    • 1970-01-01
    • 1970-01-01
    • 2022-01-27
    • 2021-11-07
    • 2020-01-24
    相关资源
    最近更新 更多