zoukankan      html  css  js  c++  java
  • Laravel 5.8 做个知乎 15 —— 站内信通知

    1 创建通知表

    php artisan notifications:table
    php artisan migrate

    2 代码

    2.1 appNotificationsNewUserFollowNotinfication.php

    php artisan make:notification NewUserFollowNotinfication
    use IlluminateSupportFacadesAuth;
        public function via($notifiable)
        {
            //        return ['mail'];   //邮件通知
            return ['database']; //站内信
        }
        
        public function toDatabase($notifiable)
        {
            return [
              'name'=> Auth::guard('api')->user()->name,
            
            ];
        }

    2.2 appHttpControllersApiFollowersController.php

    use AppNotificationsNewUserFollowNotinfication;
        public function follow()
        {
            $userToFollow = $this->user->byId(request('user'));
            
            $followed = Auth::guard('api')->user()->followThisUser($userToFollow->id);
            
            if (count($followed['attached']) > 0 ){
                $userToFollow->increment('followers_count');
                $userToFollow->notify(new NewUserFollowNotinfication());
                return response()->json(['followed'=>true]);
            }
            $userToFollow->decrement('followers_count');
            return response()->json(['followed'=>false]);
        }

    notifications表里就会有新增一行

    3 显示代码

    3.1 appHttpControllersNotificationsController.php

    php artisan make:controller NotificationsController
    <?php
    
    namespace AppHttpControllers;
    
    use IlluminateHttpRequest;
    use IlluminateSupportFacadesAuth;
    
    class NotificationsController extends Controller
    {
        //
        public function index()
        {
            $user = Auth::user();
            return view('notifications.index',compact('user'));
        }
    }

    3.2 outesweb.php

    Route::get('notifications','NotificationsController@index');

    3.3 esourcesviews otificationsindex.blade.php

    @extends('layouts.app')
    
    @section('content')
        <div class="container">
            <div class="row justify-content-center">
                <div class="col-md-8">
                    <div class="card">
                        <div class="card-header">消息通知</div>
    
                        <div class="card-body">
                           @foreach($user->notifications as $notification)
                                @include('notifications.'.snake_case(class_basename($notification->type)))
                           @endforeach
                        </div>
                    </div>
                </div>
            </div>
        </div>
    @endsection

    3.4 esourcesviews otifications ew_user_follow_notinfication.blade.php

    <li class="notifications">
        <a href="{{ $notification->data['name'] }}">
            {{ $notification->data['name'] }}
        </a> 关注了您哦!
    </li>
  • 相关阅读:
    谈谈架构层级的“开闭原则”
    将MySQL数据库中的表结构导入excel 或word
    淘宝网-软件质量属性分析
    架构漫谈阅读有感
    机器学习-分类算法之决策树、随机森林
    机器学习-分类算法之逻辑回归
    机器学习-朴素贝叶斯算法
    机器学习-分类算法之k-近邻
    机器学习-模型选择
    机器学习-scikit-learn数据集
  • 原文地址:https://www.cnblogs.com/polax/p/15056949.html
Copyright © 2011-2022 走看看