0

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.

4
  • 1
    Name your class DueForMaint (and DueForMaint.php) so the auto-loader can find it, and be sure to have a use App\Notifications\DueForMaint in the controller to prevent any namespacing issues. Commented Sep 20, 2017 at 13:29
  • 2
    You're posting docs for 5.5, tagged 5.3 but titled 5.4, which version are you using!? They are all very different. Commented Sep 20, 2017 at 13:29
  • my bad, it's 5.4 Commented Sep 20, 2017 at 13:35
  • 1
    @MGS, make sure you aren't using the 5.5 docs then, many small changes have been made to things. You can select the version at the top right, always make sure you're using the correct version. Commented Sep 20, 2017 at 13:55

2 Answers 2

1

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!

Sign up to request clarification or add additional context in comments.

Comments

0

Just add the missing property to your notification class :

private $truck;

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

1 Comment

Wonderful, Thanks alot for that quick and accurate response @Maraboc

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.