朱纯树博客
VPS测评推荐网站
cloudacead cloudacead

Laravel 简单实现消息通知

除了支持 发送邮件之外,laravel 还支持通过多种频道发送通知,包括邮件、短信 (通过 Nexmo),以及 Slack。通知还能存储到数据库以便后续在 Web 页面中显示。

  • 下面只记录了常用的邮件通知和 database 保存.

 

1. 保存到数据库

  • 创建一个通知类
PHP artisan make:notification TestNotify

这条命令会在 app/Notifications 目录下生成一个新的通知类。这个类包含 via 方法以及一个或多个消息构建的方法 (比如 toMail 或者 toDatabase) ,它们会针对指定的渠道把通知转换为对应的消息

  • TestNotify 类内容如下
    <?php
    namespace App\TestNotify;
    use Illuminate\Bus\Queueable;
    use Illuminate\Notifications\Notification;
    use Illuminate\Contracts\Queue\ShouldQueue;
    use Illuminate\Notifications\Messages\MailMessage;
    class TestNotify extends Notification implements ShouldQueue
    {
      use Queueable;
      public function __construct()
      {
      }
    
     //对应发送的通道
      public function via($notifiable)
      {
          return $notifiable->db ? ['database'] : ['mail'];
      }
    
      public function toMail($notifiable)
      {
    
      }
    
      //存储数据到表,data字段,json格式保存
      public function toArray($notifiable)
      {
          return [
              "name" => "test",
              "age" => "test",
              "id" => $notifiable->id, //$notifiable 实例实际上就是User
          ];
      }
    }
  • 创建通知表 notifications
    php artisan notifications:table || php artisan migrate
  • 控制器中发起通知, 下面只介绍使用 Notification Facade 方式
    $user =User::find(1);
    $user->db = true; //指定存储到数据表
    \Notification::send($user, new TestNotify());
  • 因为 TestNotify, 开启了队列,最后在执行队列命令,查看数据表验证是否成功。
Laravel 简单实现消息通知插图

  1. 邮件通知
  • 修改 TestNotify 类的 toMail 方法
    public function toMail($notifiable)
      {
         return (new MailMessage)
           ->subject("laravel通知")
           ->line('你好,这是一条测试通知')
           ->action('请查看', url('/'))
           ->line('打扰了'.$notifiable->id);
      }
  • 同上调用通知类
    $user =User::find(2);
    \Notification::send($user, new TestNotify());
  • 如果有必要指定请指定邮箱字段
    class User extends Model
    {
      public function routeNotificationForMail() {
          return $this->email;// 指定邮件接收者
      }
    }
  • 查看邮件,搞定!
Laravel 简单实现消息通知插图1
赞(2) 打赏
未经允许不得转载:VPS测评推荐网站 - 朱纯树博客 » Laravel 简单实现消息通知

评论 7

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
  1. #0

    I loved as much as you will receive carried out right here. The sketch is tasteful, your authored subject matter stylish. nonetheless, you command get bought an edginess over that you wish be delivering the following. unwell unquestionably come further formerly again since exactly the same nearly very often inside case you shield this hike.

  2. #0

    Usually I do not read post on blogs, but I wish to say that this write-up very forced me to try and do it! Your writing style has been amazed me. Thanks, very nice post. ラ ブ ド ー ル

  3. #0

    I am often to blogging and i actually recognize your content. The article has actually peaks my interest. I am going to bookmark your web site and keep checking for brand spanking new information.

  4. #0

    Given the serious impact of the drug on the production of semen in the male testicles, this is especially important for athletes who systematically use anabolics viagra jelly for sale uk Patients with monostotic fibrous dysplasia, isolated peripheral precocious puberty, neonatal liver cholestasis, or the classic McCune Albright syndrome all had the same molecular defect

    DeettyhoW2个月前 (02-06)回复
  5. #0

    before and after accutane Potentially, a long- term low dose hCG administration could result in a phenotype resembling PCOS at least in terms of an increased recruitment or accumulation of small antral follicles, which could lead to an increased harvest of mature oocytes in response to OS

    DeettyhoW2个月前 (02-02)回复
  6. #0

    The drug, like many cancer therapies, is not without serious risks clomid cost You may also wish to contact an experienced class action attorney who can help you protect your legal rights and get compensation for your injuries

    DeettyhoW2个月前 (01-28)回复
  7. #0

    极简壁纸: http://jiafusr.com/

    东南亚小野猫1年前 (2022-01-07)回复