zoukankan      html  css  js  c++  java
  • laravel notification

    mail篇

     public function via($notifiable)
    {
        return ['mail'];
    }
    

    1.新建notification类

    php artisan make:notification PostNotification
    

    2.设置路由

    //notification 注意默认发送到user模型中的email邮箱账号 所以要确认user邮箱可用
    Route::get('/notification',function(){
        $user = AppUser::find(1);
        $post = AppPost::find(2);
        $user->notify(new AppNotificationsPostNotification($post));
    });
    

    3.访问/notification 收到邮件

    4.常用设置方法 PostNotification.php

     public function toMail($notifiable)
    {
        return (new MailMessage)
            ->subject('A post published'.$this->post->title) //自定义主体
            ->success()  //定义按钮颜色
                    ->line('The introduction to the notification.')
                    ->action('Notification Action', url('/'))
                    ->line('Thank you for using our application!');
    }
    

    database篇 将通知都存储在数据库里

    1.修改PostNotification.php

    public function via($notifiable)
    {
        //return ['mail'];
        return ['database'];
    }
    

    2.创建notification迁移文件

     php artisan notifications:table
     php artisan migrate
    

    3.PostNotification.php 中可添加 toDatabase方法 如果没写的话默认用的是toArray方法

    4.修改web.php

    5.查看当前用户下的notifications

    6.新建一个notification

    php artisan make:notification UserSubscribe
    

    7.UserSubscribe.php 修改如下

    public function via($notifiable)
    {
        return ['database'];
    }
    
    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            'subscribed_at' => Carbon::now()
        ];
    }
    

    8.修改web.php

    //notification
    Route::get('/notification', function () {
        $user = AppUser::find(1);
        $post = AppPost::find(2);
        //$user->notify(new AppNotificationsPostNotification($post));
        $user->notify(new AppNotificationsUserSubscribe());
    });
    

    9.再次查看当前用户的notifications

    10.列出未读notifications并标识为已读

    web.php

    //notification
    Route::get('/show-notification', function () {
        return view('notifications.index');
    });
    
    //标识未读
    Route::delete('user/notification',function (){
        Auth::user()->unreadNotifications->markAsRead();
        return redirect()->back();
    });
    

    notifications.index.blade

    @extends('app')
    
    @section('content')
        <h1>我的通知:</h1>
        <ul>
        @foreach(Auth::user()->unreadNotifications as $notification)
            @include('notifications/'.snake_case( class_basename($notification->type) ))
        @endforeach
        </ul>
        <form action="/user/notification" method="POST">
            {{csrf_field()}}
            {{method_field('DELETE')}}
            <input type="submit" value="标识已读">
        </form>
    @stop
    

    user_subscribe.blade.php

    <h2>user</h2>
    {{$notification->data['subscribed_at']['date']}}
    

    post_notification.blade.php

    <h2>post</h2>
    <li>{{$notification->data['title']}}</li>
    

    标识某条已读

    $user->refresh()->unreadNotifications->where('id','57bb0e0e-8d35-4da8-850b-121a5317c9b9')->first()->markAsRead();
    

    总结:

    database

    • php artisan make:notification someNotification
    • 对于需要传入的参数做修改 例如依赖模式 Post $post
    • php artisan notification:table
    • 获取notification $user->notifications
    • 标识已读 所有的 $user->unreadNotifications->markAsRead()
      单条标识:$user->refresh()->unreadNotifications->where('id','57bb0e0e-8d35-4da8-850b-121a5317c9b9')->first()->markAsRead();
  • 相关阅读:
    R语言代写实现 Copula 算法建模依赖性案例分析报告
    R语言代写回归中的Hosmer-Lemeshow拟合优度检验
    ggplot2如何在R语言代写中绘制表格
    用SAS代写进行泊松,零膨胀泊松和有限混合Poisson模型分析
    R语言代写实现有限混合模型建模分析
    R语言代写使用混合模型进行聚类
    BZOJ 4370: [IOI2015]horses马 线段树+贪心+对数
    luoguP5824 十二重计数法 组合+生成函数+第二类斯特林数
    BZOJ 5296: [Cqoi2018]破解D-H协议 BSGS
    BZOJ 2242: [SDOI2011]计算器 BSGS
  • 原文地址:https://www.cnblogs.com/webskill/p/7464667.html
Copyright © 2011-2022 走看看