0

I'm subscribing to notification events using a listener as described in the docs.

My issue is, when I try to query the notification, for example, using $event->notification it appears to not work. This is because a print_r reveals all the properties are protected. Am I passing information to my notification incorrectly? I set the properties as public.

Notification:

<?php

namespace App\Notifications;

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

class NewAsset extends Notification
{
    use Queueable;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
     public $asset;
     public $user;

    public function __construct($asset,$user)
    {
        $this->asset = $asset;
        $this->user = $user;
    }

    /**
     * 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 [
        'type' => 'NewAsset',
        'asset_id' => $this->asset->id,
        'user_name' => $this->user->name,
        'user_id' => $this->user->id
    ];
    }
}

Notification Call:

Notification::send($users, new NewAsset($asset,Auth::user()));

Notice the payload, when I look for it here (in the listener):

public function handle(NotificationSent $event)
{
Log:info('Notification Listener:'.' '.print_r($event->notification,true));
}

I get this:

[asset] => App\Models\Asset Object
        (
            [connection:protected] => mysql
            [table:protected] => 
            [primaryKey:protected] => id
            [keyType:protected] => int
            [incrementing] => 1
            [with:protected] => Array
                (
                )

            [withCount:protected] => Array
                (
                )

            [perPage:protected] => 15
            [exists] => 1
            [wasRecentlyCreated] => 1
            [attributes:protected] => Array
                (
                    [user_id] => 1
                    [type] => Text
                    [content] => :'(
                    [wall_id] => 1
                    [updated_at] => 2018-03-06 09:36:07
                    [created_at] => 2018-03-06 09:36:07
                    [id] => 737
                )

            [original:protected] => Array
                (
                    [user_id] => 1
                    [type] => Text
                    [content] => :'(
                    [wall_id] => 1
                    [updated_at] => 2018-03-06 09:36:07
                    [created_at] => 2018-03-06 09:36:07
                    [id] => 737
                )

            [changes:protected] => Array
                (
                )

            [casts:protected] => Array
                (
                )

            [dates:protected] => Array
                (
                )

            [dateFormat:protected] => 
            [appends:protected] => Array
                (
                )

            [dispatchesEvents:protected] => Array
                (
                )

            [observables:protected] => Array
                (
                )

            [relations:protected] => Array
                (
                )

            [touches:protected] => Array
                (
                )

            [timestamps] => 1
            [hidden:protected] => Array
                (
                )

            [visible:protected] => Array
                (
                )

            [fillable:protected] => Array
                (
                )

            [guarded:protected] => Array
                (
                    [0] => *
                )

        )

For user they are also all protected, not included here because it has email data and the like. So how can/should I pass data to my notifications that I can actually query?

Summer Developer
  • 2,056
  • 7
  • 31
  • 68

0 Answers0