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();

laravel notification的更多相关文章

  1. laravel 自带消息notification通知

    原文地址:https://blog.csdn.net/zhangxh1013/article/details/53130490

  2. [php]laravel框架容器管理的一些要点

    本文面向php语言的laravel框架的用户,介绍一些laravel框架里面容器管理方面的使用要点.文章很长,但是内容应该很有用,希望有需要的朋友能看到.php经验有限,不到位的地方,欢迎帮忙指正. ...

  3. Laravel系列 目录结构

    Where Is The Models Directory? app directory by default 其中 app:,core code of your application, almos ...

  4. 【转】下载量最高的 100 个 Laravel 扩展包推荐

    说明 Laravel 另一个令人喜欢的地方,是拥有活跃的开发者社区,而活跃的开发者社区带来的,是繁华的扩展包生态. 本文对 Packagist 上打了 Laravel 标签 的扩展包进行整理,截止到现 ...

  5. get started with laravel

    Browsing the API (http://laravel.com/api) can be somewhat intimidating at first.But it is often the ...

  6. laravel扩展包开发步骤总结

    1. 创建包 php artisan workbench vendor/package --resources     注:  vendor:开发商名   package:包名   2.修改下包里co ...

  7. 【社交系统研发日记】如何在 Laravel 中 “规范” 的开发验证码发送功能

    顺便发个小通知:7月15日ThinkSNS+开源版发布,同时非开源的APP也走出内测阶段,体验二维码也全面发布体验. 什么是ThinkSNS ? ThinkSNS(简称TS),一款全平台综合性社交系统 ...

  8. Laravel 和 Spring Boot 两个框架比较创业篇(一:开发效率)

    我个人是比较不喜欢去正儿八经的比较两个框架的,这样没有意义,不过欲善其事先利其器! 技术是相通的,但是在某个特定的领域的某个阶段肯定有相对最适合的一个工具! 这里比较不是从技术角度比较,而是从公司技术 ...

  9. [ Laravel 5.5 文档 ] 快速入门 —— 目录结构篇

    简介 Laravel 默认的目录结构试图为不管是大型应用还是小型应用提供一个良好的起点.当然,你也可以按照自己的喜好重新组织应用的目录结构,因为 Laravel 对于指定类在何处被加载没有任何限制 — ...

随机推荐

  1. Tinking in Java ---Java的NIO和对象序列化

    前面一篇博客的IO被称为经典IO,因为他们大多数都是从Java1.0开始就有了的:然后今天这篇博客是关于NIO的,所以的NIO其实就是JDK从1.4开始,Java提供的一系列改进的输入/输出处理的新功 ...

  2. java web 学习笔记 - servlet01

    ---恢复内容开始--- 1.Servlet介绍 Servlet 是用java语言编写的服务器端小程序,属于一个CGI程序,但与传统的CGI不同的是,它是多线程实现的,并且可以多平台移植. 用户自定义 ...

  3. cat - 连接文件并在标准输出上输出

    SYNOPSIS 总览 cat [选项列表] [文件列表]... DESCRIPTION 描述 将文件列表中的文件或标准输入连接到标准输出. -A, --show-all 等价于 -vET . -b, ...

  4. vue之loader处理静态资源

    webpack 是利用loader 来处理各种资源的,wepback的配置基本上就是为各种资源文件,指定不同类型的loader. 1,处理css 最基本的css 处理loader 是css-loade ...

  5. wdcp 打开网页显示 Apache 2 Test Page powered by CentOS -- 来自辉哥博客

    是因为更新过系统,安装并更新了系统自带的apache 执行这个命令即可 #ln -sf /www/wdlinux/init.d/httpd /etc/rc.d/init.d/httpd#reboot ...

  6. 【BIEE】新建用户,并且赋予组BIconsumer,访问BIpublisher报表报错:检索数据xml时出错

    问题描述 今天新建一个用户用户查看报表,并且赋予该用户属于BIConsumer组,但是在访问报表的时候出现以下两个错: 1.xdo格式类的报表 2.RTF模板制作的报表 解决方案: 出现这个问题的原因 ...

  7. secureCRT 破解

    转自:http://www.cnblogs.com/qingtingzhe/articles/5008902.html

  8. BZOJ3545 Peaks 离线处理+线段树合并

    题意: 在Bytemountains有N座山峰,每座山峰有他的高度h_i.有些山峰之间有双向道路相连,共M条路径,每条路径有一个困难值,这个值越大表示越难走,现在有Q组询问,每组询问询问从点v开始只经 ...

  9. [Python3网络爬虫开发实战] 2.2-网页基础

    用浏览器访问网站时,页面各不相同,你有没有想过它为何会呈现这个样子呢?本节中,我们就来了解一下网页的基本组成.结构和节点等内容. 1. 网页的组成 网页可以分为三大部分——HTML.CSS和JavaS ...

  10. DB2隔离级别

    四.隔离级别与锁 数据库是利用锁和隔离级别来共同处理数据库的并发的.DB2数据库用来尝试实施并发性的方法之一是通过使用隔离级别,它决定在第一个事务访问数据时,如何对其他事务锁定或隔离该事务所使用的数据 ...