laravel 消息通知实现

本文详细介绍了在 Laravel 中如何实现消息通知功能,包括通过迁移创建数据库,生成消息通知类,触发通知,模板展示及通知清空。重点讨论了通知类的创建、通知频道的选择以及如何在模板中显示和处理通知。
摘要由CSDN通过智能技术生成

PS:本次主要记录一下laravel 自带的消息通知Notification的实现

1.生成数据库,可以使用迁移

  • 在项目目录下的cmd中运行 artisan命令
php artisan notifications:table
php artisan migrate
  • 在user表里增加一个notification_count 字段,记录未读通知数量

2.生成消息通知类

这里模拟点赞通知

php artisan make:notification UserSupport

如果implements ShouldQueue这个接口的话就会异步队列执行,如果去掉的话就是同步执行。

<?php

/**
 * 模拟用户点赞通知
 * laravel 通知表  notifications
 * 点赞表 user_support
 * 作者表 users
 * 用户表 user_info
 */

namespace App\Notifications;

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

use App\Models\UserSupport as UserSupportModel;

class UserSupport extends Notification implements ShouldQueue
{
   
    use Queueable;

    public $support;
   
     public function __construct(UserSupportModel $support)
    {
   
        // 注入回复实体,方便 toDatabase 方法中的使用
        $this->support = $support;
    }

    public function via($notifiable)
    {
   	
    	// 开启通知的频道
        return ['mail','database'];
    }

    public function toMail($notifiable)
    {
   
        return (new MailMessage)
                    ->line('The introduction to the notification.')
                    ->action('Notification Action', 'www.baidu.com')
                    ->line('Thank you for using our application!');
    }

    public function toDatabase
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小阿巳

你的鼓励就是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值