我按照文档中的说明使用监听器订阅notification events。
我的问题是,当我尝试查询通知时,例如,使用$event->notification
它似乎无效。这是因为print_r
显示所有属性都受到保护。我是否错误地将信息传递给我的通知?我将属性设置为公共。
通知:
<?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::send($users, new NewAsset($asset,Auth::user()));
注意有效负载,当我在这里查找(在监听器中):
public function handle(NotificationSent $event)
{
Log:info('Notification Listener:'.' '.print_r($event->notification,true));
}
我明白了:
[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] => *
)
)
对于user
,它们也都是protected
,此处未包含,因为它包含电子邮件数据等。那么我怎样才能/应该将数据传递给我实际可以查询的通知?