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.
DueForMaint(andDueForMaint.php) so the auto-loader can find it, and be sure to have ause App\Notifications\DueForMaintin the controller to prevent any namespacing issues.