美文网首页
Laravel 的消息通知系统

Laravel 的消息通知系统

作者: 云龙789 | 来源:发表于2018-03-08 20:59 被阅读908次

    手册-Laravel 的消息通知系统
    收费教程
    最简单易懂的laravel事件,这个功能非常的有用

    1.生成数据库

    $ php artisan notifications:table
    
    $ php artisan migrate
    

    生成的表是notifications 数据结构是固定的

    2.应用-生成话题回复通知类

    $ php artisan make:notification TopicReplied
    

    3. 触发通知

    写在模型监控器里 --app/Observers/ReplyObserver.php

    <?php
    .
    .
    .
    
    use App\Notifications\TopicReplied;
    
    class ReplyObserver
    {
        public function created(Reply $reply)
        {
            $topic = $reply->topic;
            $topic->increment('reply_count', 1);
    
            // 通知作者话题被回复了
            $topic->user->notify(new TopicReplied($reply));
        }
    
        .
        .
        .
    }
    

    app/Models/User.php

    <?php
    .
    .
    .
    
    use Auth;
    
    class User extends Authenticatable
    {
        use Notifiable {
    // Notifiable 的trait里面有notify方法,此处我们给它个别名
            notify as protected laravelNotify;
        }
        public function notify($instance)
        {
            // 如果要通知的人是当前用户,就不必通知了!
            if ($this->id == Auth::id()) {
                return;
            }
            $this->increment('notification_count');  //字段加1
            $this->laravelNotify($instance); // 发送通知
        }
    
        .
        .
        .
    }
    

    其实我们引用的是RoutesNotifications类notify方法

    public function notify($instance)
        {
            app(Dispatcher::class)->send($this, $instance);
        }
    
    如果直接使用
    Notification::send($users,new TopicReplied($reply));
    

    框架内获取通知内容的trait文件

    <?php
    
    namespace Illuminate\Notifications;
    
    trait HasDatabaseNotifications
    {
        /**
         * Get the entity's notifications.
         */
        public function notifications()
        {
            return $this->morphMany(DatabaseNotification::class, 'notifiable')
                                ->orderBy('created_at', 'desc');
        }
    
        /**
         * Get the entity's read notifications.
         */
        public function readNotifications()
        {
            return $this->notifications()
                                ->whereNotNull('read_at');
        }
    
        /**
         * Get the entity's unread notifications.
         */
        public function unreadNotifications()
        {
            return $this->notifications()
                                ->whereNull('read_at');
        }
    }
    
    

    测试获取数据

    $user = User::find(1);
    $test = $user->notifications;
    dd($test->toArray());
    
    
    已读未读是有一个 read_at 字段
    $user->unreadNotifications; // 获取所有未读通知
    $user->readNotifications; // 获取所有已读通知
    $user->notifications; // 获取所有通知
    
    
    
    图片.png

    4. 清除未读消息标示

    <?php
    .
    .
    .
    
    class User extends Authenticatable
    {
        .
        .
        .
    
        public function markAsRead()
        {
            $this->notification_count = 0;
            $this->save();
            $this->unreadNotifications->markAsRead();
        }
    }
    

    使用

    修改控制器的 index() 方法,新增清空未读提醒的状态:

    app/Http/Controllers/NotificationsController.php

    <?php
    .
    .
    .
    class NotificationsController extends Controller
    {
        .
        .
        .
        public function index()
        {
            // 获取登录用户的所有通知
            $notifications = Auth::user()->notifications()->paginate(20);
            // 标记为已读,未读数量清零
            Auth::user()->markAsRead();
            return view('notifications.index', compact('notifications'));
        }
    }
    

    这个handle方法就是我们要做的具体实现了,有个很方便的功能就是如果implements ShouldQueue这个接口的话就会异步队列执行,如果去掉的话就是同步执行。

    注册的时候其实建议使用这种方式

    <?php
    
    namespace App\Providers;
    
    use Laravel\Lumen\Providers\EventServiceProvider as ServiceProvider;
    
    class EventServiceProvider extends ServiceProvider
    {
        /**
         * The event listener mappings for the application.
         *
         * @var array
         */
        protected $listen = [
            // 用户注册后的事件
            'App\Events\Register' => [
                // 发送广告邮件
                'App\Listeners\SendAdMail',
                // 发送短信
                'App\Listeners\SendSms',
                // 发送帮助信息
                'App\Listeners\SendHelpInformation',
    
            ],
        ];
    }
    

    然后执行执行php artisan event:generater 直接会在相应的目录生成相应的这四个类

    相关文章

      网友评论

          本文标题:Laravel 的消息通知系统

          本文链接:https://www.haomeiwen.com/subject/ccfufftx.html