如何将Laravel通知与我的网站集成?

时间:2018-07-24 10:48:40

标签: php database laravel notifications

我最近完成了使用Laravel设置数据库通知的任务。我现在可以发送通知。但是,我现在想要做的是:发送通知时,我希望图标出现在我的网站上,用户可以将鼠标悬停在该图标上并查看通知标题,或单击该图标阅读全部内容。这可能吗?

此外,有没有一种方法可以让我的数据库查看已发送的所有通知?也许使用MySQL。

2 个答案:

答案 0 :(得分:1)

第一个问题:

是的,您可以像这样为每个用户获取通知:

在您要向每个用户传递通知结果的控制器中

$user = App\User::find(1);
   foreach ($user->unreadNotifications as $notification) {
   echo $notification->type;
}

对于第二个问题,您应该向toArray函数添加一个值,如th 而且,您可以计算通知并在需要帮助Ajax调用的任何位置或每个刷新html页面的标题上显示消息,

在通知文件中

public function toArray($notifiable)
{
    return [
        'sender_id' => $this->user->id,
    ];
}

此后,当您要获取此数据(在通知表上查询以查找您发送的通知)时,可以使用JSON Where Clauses

希望它会对您有所帮助,

答案 1 :(得分:0)

获取最新的通知和计数非常容易。.尽管唯一的区别是您如何知道所有通知中都有新的通知。因此,我建议您在数据库中使用这种设计。

数据库

[id, name, latest]

注意:最新的colunm是整数类型。 0或1,其中0不是最新的,1不是最新的。

插入通知 因此,当您插入数据时,将新通知保存在数据库中,例如最新的

$notification = new Notification();
$notification->name = 'whatever';
$notification->latest = 1; // important part
$notification->save()

获取最新通知并显示铃声

$latest_notification = Notification::where('latest', 1)->first(); // latest
//before responding to view - save notification as 0
$latest_notification->latest = 0;
$latest_notification->save();

if(latest_notification->isNotNull) {
return["latest" => latest_notification]
}else{
return["latest" => 0];
}

前端显示铃铛

<span id="notifier"></span>

JavaScript-jQuery //如果您的回应不为0,则您会收到通知 //在向服务器发布请求或axios发送请求后,无论使用什么

if(response.latest !== 0) {
$('#notifier').html('<i class="fas fa-bell"></i>' + '1')
} else {
$('#notifier').html('0')
}

对不起,您还没有时间进行测试,但是可以肯定,这是您最好的地方。

相关问题