我正在使用Laravel Pusher通知,并能够在创建行时添加通知,但在更新行时遇到困难。下面是下面的代码
CategoryObserver
public function updated(Category $category)
{
$user = Auth::user();
foreach($user->followers as $follower)
{
$follower->notify(new UpdateCategory($user, $category));
}
}
和UpdateCategory
class UpdateCategory extends Notification
{
use Queueable;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct()
{
//
}
public function via($notifiable)
{
return ['database'];
}
public function toDatabase($notifiable)
{
return [
'following_id' => Auth::user()->id,
'following_name' => Auth::user()->name,
];
}
public function toArray($notifiable)
{
return [
'id' => $this->id,
'read_at' => null,
'data' => [
'following_id' => Auth::user()->id,
'following_name' => Auth::user()->name,
],
];
}
}
发布于 2018-06-03 01:39:20
public function updated(Category $category)
{
$user = Auth::user();
foreach($user->followers as $follower)
{
$follower->notify(new UpdateCategory($user, $category));
}
}
//在通知中
class UpdateCategory extends Notification
{
use Queueable;
public $user;
public $category;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct(User $user, Category $category)
{
//
$this->user = $user;
$this->category = $category;
}
public function via($notifiable)
{
return ['database'];
}
public function toDatabase($notifiable)
{
return [
'following_id' => $this->user->id,
'following_name' => $this->user->name,
];
}
public function toArray($notifiable)
{
return [
'id' => $this->id,
'read_at' => null,
'data' => [
'following_id' => $this->user->id,
'following_name' => $this->user->name,
],
];
}
}
您正在无法访问的可排队通知中使用Auth()
。从通知中删除可排队的特征,并尝试在通知类中注入用户对象。
https://stackoverflow.com/questions/50659216
复制相似问题