I have the following error:
Undefined property: Illuminate\Notifications\Events\NotificationSent::$user in /var/www/app/app/Listeners/NoticationListener.php:31
The error occurs here:
<?php
namespace App\Listeners;
use Illuminate\Notifications\Events\NotificationSent;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
class NoticationListener implements ShouldQueue
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param NotificationSent $event
* @return void
*/
public function handle(NotificationSent $event)
{
$notification = $event->notifiable;
$addressee = $notification; //error here
$address = $notification;
$type = "Notification";
dispatch(new SendEmail($type,$addressee,$address));
}
}
I don't understand this undefined property, especially on this line. How can I dd() from here? I tried to log the $event but I couldn't get it to log, only got this error.
My notifications work very well in the application, I just want an email to accompany them, which is why I have this event-listener/job.
Thank you.
EDIT
The repository code that is dispatching the notification is below:
public function notify($asset)
{
$users = User::where("id","!=",Auth::user()->id)->get();
Notification::send($users, new NewAsset($asset));
}
That extension of the Notification class is below:
class NewAsset extends Notification
{
use Queueable;
/**
* Create a new notification instance.
*
* @return void
*/
protected $asset;
public function __construct($asset)
{
$this->asset = $asset;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['database'];
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
'asset_id' => $this->asset->id
];
}
}
EDIT 2
If someone could advise how to do some error checking at this juncture, that could be helpful going forward. Because the code is async on the server it isn't returning data to the client, and when I try to get it to Log it doesn't seem to do so before it gets caught in the error.
How can I go about debugging in this scenario?
I've looked at the framework's source code and have no idea where the $user property is coming from. I assume it has to do with $event->notifiable being tied to the User model, but if it fires correctly for all affected users from within the application, why would its property be undefined in this context?
Please assist, thank you.