简体   繁体   中英

Using Notifications in Laravel 5.4

I've been trying to implement Notifications into my App, by storing them in the database as detailed in the documentation here: https://laravel.com/docs/5.5/notifications

This is my Truck model with the notifiable trait referenced

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

use Illuminate\Notifications\Notifiable;

use App\Driver;

class Truck extends Model
{
    use Notifiable;
}

Here is the notification class I use named 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,
        ];
    }
}

Here is my a function in my truck controller called notifi_me method which I use to create the notifications

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



}

Whenever I call the notifi_me function, I keep getting this error:

Undefined property: App\\Notifications\\due_for_maint::$truck

Could someone explain why this is the case and how I can fix it? My understanding is that Laravel establishes a relationship between the Truck object and the notification, which should make the trucks attributes referencable using syntax like $this->truck->id in the notification class.

The notifiable parameter in the toMail and toArray functions is the truck itself, since that's what you call notify on. You don't have to do anything in the constructor, just replace

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

with

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

and you're good to go!

Just add the missing property to your notification class :

private $truck;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM