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 = \App\User::find(1);
$post = \App\Post::find(2);
$user->notify(new \App\Notifications\PostNotification($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 = \App\User::find(1);
$post = \App\Post::find(2);
//$user->notify(new \App\Notifications\PostNotification($post));
$user->notify(new \App\Notifications\UserSubscribe());
});

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();
posted @ 2017-09-01 18:45  miyaye  阅读(1056)  评论(0)    收藏  举报