【问题标题】:Using Notifications in Laravel 5.4在 Laravel 5.4 中使用通知
【发布时间】:2018-03-01 14:11:07
【问题描述】:

我一直在尝试将通知实现到我的应用程序中,方法是将它们存储在数据库中,如此处文档中所述:https://laravel.com/docs/5.5/notifications

这是我的卡车模型,引用了可通知的特征

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

use Illuminate\Notifications\Notifiable;

use App\Driver;

class Truck extends Model
{
    use Notifiable;
}

这是我使用的名为due_for_maint的通知类

<?php

namespace App\Notifications;

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

class due_for_maint extends Notification
{
    use Queueable;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

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

    /**
     * Get the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        return (new MailMessage)
                    ->line('The introduction to the notification.')
                    ->action('Notification Action', url('/'))
                    ->line('Thank you for using our application!');
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
          'truck_no' => $this->truck->registrationNo,
          'Milage' => $this->truck->readingMilage,
        ];
    }
}

这是我的卡车控制器中的一个函数,称为 notifi_me 方法,我用它来创建通知

public function notifi_me(){
$truck = Truck::find(10);
$truck->notify(new due_for_maint($truck));



}

每当我调用 notifi_me 函数时,我都会不断收到此错误:

未定义属性:App\Notifications\due_for_maint::$truck

有人能解释一下为什么会出现这种情况以及我该如何解决吗? 我的理解是 Laravel 建立了 Truck 对象和通知之间的关系,这应该使卡车属性可以使用通知类中的 $this->truck->id 之类的语法来引用。

【问题讨论】:

  • 将您的类命名为DueForMaint(和DueForMaint.php)以便自动加载器可以找到它,并确保在控制器中有use App\Notifications\DueForMaint 以防止出现任何命名空间问题。
  • 您正在发布 5.5 的文档,标记为 5.3 但标题为 5.4,您使用的是哪个版本!?它们都非常不同。
  • 我的错,它是 5.4
  • @MGS,然后确保您没有使用 5.5 文档,已经对事物进行了许多小的更改。您可以在右上角选择版本,请始终确保您使用的是正确的版本。

标签: php laravel laravel-5 notifications laravel-5.4


【解决方案1】:

toMail 和 toArray 函数中的可通知参数是卡车本身,因为这就是您所说的 notify。你不必在构造函数中做任何事情,只需替换

return [
    'truck_no' => $this->truck->registrationNo,
    'Milage' => $this->truck->readingMilage,
];

return [
    'truck_no' => $notifiable->registrationNo,
    'Milage' => $notifiable->readingMilage,
];

你很高兴!

【讨论】:

    【解决方案2】:

    只需将缺少的属性添加到您的通知类:

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

    【讨论】:

    • 太好了,非常感谢@Maraboc 的快速准确回复
    猜你喜欢
    • 2017-08-12
    • 2018-01-06
    • 2017-07-02
    • 2018-01-02
    • 2017-12-30
    • 2017-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多