1.比如收到关注的时候的通知,同时发送通知邮件:
参考:
Learn How to Send Beautiful Mail Notifications in Laravel
Laravel 5.7 - New Notification System Tutorial for Beginner
系列讲解:
Laravel技巧集锦(29):使用vuejs组件化开发关注按钮
Laravel技巧集锦(31):使用vuejs组件化开发关注作者按钮
Laravel技巧集锦(32):使用notification实现站内通知(数据库)
Laravel技巧集锦(33):使用notification实现站内通知(邮件)
Laravel技巧集锦(34):使用vuejs组件化开发点赞按钮
Laravel技巧集锦(35):使用vuejs组件化开发发送私信功能
Laravel技巧集锦(39):使用notification实现站内通知(私信通知)
示例项目:
Laravel Notifications – The Ultimate Guide
扩展包:
使用Notification:
执行
1 php artisan make:notification NewUserFollowNotification
再执行
1 php artisan notifications:table
生成一个官方提供的notifications的数据库迁移文件
执行
1 php artisan migrate
修改NewUserFollowNotification:
1 <?php 2 3 namespace App\Notifications; 4 5 use App\User; 6 use Illuminate\Bus\Queueable; 7 use Illuminate\Contracts\Queue\ShouldQueue; 8 use Illuminate\Notifications\Messages\MailMessage; 9 use Illuminate\Notifications\Notification; 10 11 class NewUserFollowNotification extends Notification 12 { 13 use Queueable; 14 /** 15 * @var User 16 */ 17 private $user; 18 19 /** 20 * Create a new notification instance. 21 * 22 * @param $user 23 */ 24 public function __construct($user) 25 { 26 // 27 $this->user = $user; 28 } 29 30 /** 31 * Get the notification‘s delivery channels. 32 * 33 * @param mixed $notifiable 34 * @return array 35 */ 36 public function via($notifiable) 37 { 38 return [‘database‘]; 39 } 40 41 /** 42 * Get the mail representation of the notification. 43 * 44 * @param mixed $notifiable 45 * @return \Illuminate\Notifications\Messages\MailMessage 46 */ 47 public function toMail($notifiable) 48 { 49 return (new MailMessage) 50 ->line(‘The introduction to the notification.‘) 51 ->action(‘Notification Action‘, url(‘/‘)) 52 ->line(‘Thank you for using our application!‘); 53 } 54 55 /** 56 * Get the array representation of the notification. 57 * 58 * @param mixed $notifiable 59 * @return array 60 */ 61 public function toArray($notifiable) 62 { 63 return [ 64 // 65 ]; 66 } 67 68 69 /** 70 * 添加一个方法,方法名要与via中添加的database 一致 to加上Database 71 * @param $notifiable 72 * @return array 73 */ 74 public function toDatabase($notifiable) 75 { 76 return [ 77 //要记录的数据 78 ‘name‘ => $this->user->name, 79 ]; 80 } 81 } 82 83
修改FollowerController,添加通知被关注用户的逻辑代码:
1 <?php 2 3 namespace App\Http\Controllers; 4 5 use App\Notifications\NewUserFollowNotification; 6 use App\User; 7 use Illuminate\Http\Request; 8 use Illuminate\Support\Facades\Notification; 9 10 class FollowerController extends Controller 11 { 12 // 13 public function __construct() 14 { 15 $this->middleware(‘auth‘) 16 ->except( 17 [ 18 ‘getFollowStats‘, 19 ‘followThroughApi‘, 20 ] 21 ); 22 } 23 24 25 public function getFollowStats(Request $request) 26 { 27 //解析出用户 28 $user = User::find($request->get(‘user‘)); 29 //解析出当前登录用户 30 $currentUser = auth()->user(); 31 //返回能否关注的状态 [follow是FollowPolicy中的follow方法,FollowPolicy已经在AuthServiceProvider中注册] 32 return response()->json( 33 [ 34 ‘followable‘ => $currentUser->can(‘follow‘, $user), 35 ‘self‘ => $user->id === $currentUser->id, 36 ] 37 ); 38 } 39 40 41 public function followThroughApi(Request $request) 42 { 43 //解析出用户 44 $user = User::find($request->get(‘user‘)); 45 46 //解析出当前登录用户 47 $currentUser = User::find(auth()->user()->id); 48 49 //执行关注/取关操作 [不能关注自己] 50 if ($user->id !== $currentUser->id) { 51 //入库操作 52 $currentUser->followings()->toggle($user->id); 53 54 if (!($currentUser->can(‘follow‘, $user))) //如果关注已经入库成功[可关注状态为false],那就要通知,否则就是取关,不用通知 55 { 56 //通知被关注用户 57 Notification::send($user, new NewUserFollowNotification($currentUser)); 58 } 59 } 60 61 //返回新能否关注的状态 [follow是FollowPolicy中的follow方法,FollowPolicy已经在AuthServiceProvider中注册] 62 return response()->json( 63 [ 64 ‘followable‘ => $currentUser->can(‘follow‘, $user), 65 ‘self‘ => $user->id === $currentUser->id, 66 ] 67 ); 68 } 69 } 70 71
添加显示通知的view视图:
notifications/index.blade.php:
代码:
1 @extends(‘layouts.app‘) 2 @section(‘content‘) 3 <div class="container"> 4 <div class="row"> 5 <div class="card"> 6 <div class="card-header"> 7 你有新的消息! 8 </div> 9 <div class="card-body"> 10 @forelse($user->notifications as $notification) 11 <p class="text text-warning"> {{ snake_case(class_basename($notification->type)) }}</p> 12 @empty 13 @endforelse 14 </div> 15 </div> 16 </div> 17 </div> 18 @endsection
根据视图中
snake_case(class_basename($notification->type))
获得的返回值:new_user_follow_notification
新建一个view专用于处理本类的视图:
notifications/new_user_follow_notification.blade.php:
代码:
1 <li class="text-success"> 2 {{ $notification->data[‘name‘]."关注了你" }} 3 </li> 4 5
再修改notifications/index.blade.php:
1 @extends(‘layouts.app‘) 2 @section(‘content‘) 3 <div class="container"> 4 <div class="row"> 5 <div class="card"> 6 <div class="card-header"> 7 你有新的消息! 8 </div> 9 <div class="card-body"> 10 @forelse($user->notifications as $notification) 11 <p class="text text-warning"> 12 @include(‘notifications.‘.snake_case(class_basename($notification->type))) 13 </p> 14 @empty 15 @endforelse 16 </div> 17 </div> 18 </div> 19 </div> 20 @endsection
效果:
添加route路由及NotificationController:
web.php添加:
1 #region 2 3 //用户通知消息路由 4 Route::get(‘notifications‘, ‘NotificationController@index‘); 5 #endregion 6
web.php代码:
1 <?php 2 3 /* 4 |-------------------------------------------------------------------------- 5 | Web Routes 6 |-------------------------------------------------------------------------- 7 | 8 | Here is where you can register web routes for your application. These 9 | routes are loaded by the RouteServiceProvider within a group which 10 | contains the "web" middleware group. Now create something great! 11 | 12 */ 13 14 Route::get(‘/‘, function () { 15 return view(‘welcome‘); 16 }); 17 18 Auth::routes([‘verify‘ => true]); 19 20 Route::get(‘/home‘, ‘HomeController@index‘)->name(‘home‘); 21 22 Route::resource(‘questions‘, ‘QuestionController‘); 23 24 25 #region 回答路由CRUD 26 27 //查看回答 以及 回答的form 都是在questions详细内容页面 28 29 //提交回答 30 Route::post(‘questions/{question}/answers‘, ‘AnswerController@store‘)->name(‘answers.store‘); 31 32 //更新回答 33 34 35 //删除回答 36 37 38 #endregion 39 40 41 #region 42 //用户关注 取消关注问题 43 Route::get(‘questions/{question}/follow‘, ‘QuestionController@follow‘)->name(‘questions.follow‘); 44 #endregion 45 46 47 #region 48 49 //用户通知消息路由 50 Route::get(‘/notifications‘, ‘NotificationController@index‘)->name(‘notification.index‘); 51 #endregion 52 53
然后执行:
1 php artisan make:controller NotificationController
NotificationController代码如下:
1 <?php 2 3 namespace App\Http\Controllers; 4 5 use App\User; 6 use Illuminate\Http\Request; 7 8 class NotificationController extends Controller 9 { 10 // 11 public function index() 12 { 13 $user = auth()->user(); 14 15 return view(‘notifications.index‘, compact(‘user‘)); 16 } 17 } 18 19
Laravel Vuejs 实战:开发知乎 (24)站内信通知
原文:https://www.cnblogs.com/dzkjz/p/12398541.html