创建Post and  User模型

php artisan make:model  Post

php artisan make:model  User

创建posts and  users 表文件

php artisan make:migration create_users_table --create=users

php artisan make:migration create_users_table --create=posts

在表文件设置表结构

Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
}); Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email');
$table->string('password');
$table->string('remember_token');
$table->timestamps();
});

生成posts and  users and notifications表

php artisan notifications:table

php artisan migrate

创建测试数据

先在database/factories/ModelFactoy.php中设置需要的数据类型

$factory->define(App\User::class, function (Faker\Generator $faker) {
static $password; return [
'name' => $faker->name,
'email' => $faker->unique()->safeEmail,
'password' => $password ?: $password = bcrypt('secret'),
'remember_token' => str_random(10),
];
}); $factory->define(App\Post::class, function (Faker\Generator $faker) {
return [
'name' => $faker->name,
];
});

再执行命令

php artisan  tinker

namespace App

factory('App\User',10)->create()
 
factory('App\Post',10)->create()

创建Notifications目录,以及通知文件

创建之后即可看见InvoicePaid.php  and  UserSubscrible.php 文件

php artisan make:notification   InvoicePaid

php artisan make:notification   UserSubscrible

Formatting Database Notifications

在 notification class 中可以用   toDatabase or toArray  方法 , 将数据存入到数据库中 ,,同时 这两个方法接受$notifiable entity,并返回一个普通的数组(json 格式)。我的代码如下:

//InvoicPaid
public function toArray($notifiable)
{
return [
'post_id' => $this->id,
];
}
/// UserSubscribe
public function toArray($notifiable)
{
return [
'subscribe_at'=>Carbon::now(),// 记录时间
];
}

设置路由

Auth::LoginUsingId(2);
Route::get('/', function () {
// return view('welcome'); Auth::user()->notify(new \App\Notifications\PostPublised());
Auth::user()->notify(new \App\Notifications\UserSubscribed());
});

刷新时即可看见数据库中插入数据了,同时 read_at 字段为 null

Notification 得数据显示

在welcome.php添加如下代码,以驼峰的形式显示数据:

<h2>未读通知</h2>
<ul>
@foreach(Auth::user()->unreadNotifications as $notification)
{{-- @include('notification.'.snake_case(class_basename($notification->type)))--}} <li>{{snake_case(class_basename($notification->type))}}</li>
@endforeach
</ul>
 
<form method="post" action="/user/notification" >
{{csrf_field()}}
<button type="submit">提交</button> </form>

新建  /user/notification 路由, 把未读的通知变为已读的 ,及修改 read_at字段的值  , 第二次刷新页面就不会有数据显示 ,同时可以利用这个对应不同的 用户加载不同的模板

\Illuminate\Support\Facades\Route::post('/user/notification',function (){
\Illuminate\Support\Facades\Auth::user()->unreadNotifications->markAsRead();
});

laravel中的Database Notifications的更多相关文章

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

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

  2. Laravel 中使用 JWT 认证的 Restful API

    Laravel 中使用 JWT 认证的 Restful API 5天前/  678 /  3 / 更新于 3天前     在此文章中,我们将学习如何使用 JWT 身份验证在 Laravel 中构建 r ...

  3. 如何在 Laravel 中 “规范” 的开发验证码发送功能

    什么是ThinkSNS ? ThinkSNS(简称TS),一款全平台综合性社交系统,为国内外大中小企业和创业者提供社会化软件研发及技术解决方案,目前最新版本为ThinkSNS+(简称TS+).Thin ...

  4. laravel中的错误与日志

    日志 laravel中的日志是基于monolog而封装的.laravel在它上面做了几个事情: 把monolog中的addInfo等函数简化成为了info这样的函数 增加了useFiles和useDa ...

  5. Laravel 中使用 Redis 数据库

    一.前言 Redis 是一个开源高效的键值对存储系统,它通常用作为一个数据结构服务器来存储键值对,它可以支持字符串.散列.列表.集合.有序集合. 1. 安装 predis/predis 在 Larav ...

  6. Laravel 中缓存驱动的速度比较

    缓存是web开发中重要的一部分,我相信很多人和我一样,经常忽略这个问题. 随着工作经验的累积,我已经意识到缓存是多么的重要,这里我通过 Scotch 来解释一下它的重要性. 通过观察发现,Scotch ...

  7. 在Laravel中使用数据库事务以及捕获事务失败后的异常

    Description 在Laravel中要想在数据库事务中运行一组操作,则可以在 DB facade 中使用 transaction 方法.如果在事务的闭包内抛出异常,事务将会被自动还原.如果闭包运 ...

  8. 深入理解 Laravel 中 config 配置加载原理

    Laravel的配置加载其实就是加载config目录下所有文件配置.如何过使用php artisan config:cache则会把加载的配置合并到一个配置文件中,下次请求就不会再去加载config目 ...

  9. Laravel中使用自己的类库三种方式

    虽然Composer使得我们可以重用很多现有的类库(例如packagist.org中的),但是我们仍然可能用到一些不兼容composer的包或者类库.另外在某一项目中,我们也可能会创建某一类库,而且可 ...

随机推荐

  1. 云脉推出表格识别API接口可以自助接入

    针对如今市场上对于海量票据信息的录入需求,近期厦门云脉技术有限公司推出票据识别相关的产品与服务,更是在云脉OCR SDK开发者平台上上线表格识别API接口,供广大开发者和集成商自助接入.为了降低财务系 ...

  2. 1.4.2.1. FILES(Core Data 应用程序实践指南)

    #define debug 1 #pragma mark - FILES NSString *storeFilename = @"Grocery-Dude.sqlite";

  3. HDFS存储系统

    HDFS存储系统 一.基本概念 1.NameNode HDFS采用Master/Slave架构.namenode就是HDFS的Master架构.主要负责HDFS文件系统的管理工作,具体包括:名称空间( ...

  4. C# WInform 界面左导航菜单

    如图所示: 下载位置: http://pan.baidu.com/s/1c1uRwkw

  5. spring mvc 返回页面数据

    handler package com.stone.controller; import java.util.Map; import javax.servlet.http.HttpServletReq ...

  6. Canvas get/putImageData

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. Linux笔记(三) - 文件搜素

    (1)文件搜索:find-name  根据文件名, *匹配任意字符 ,?单个字符-iname  根据文件名, 不区分大小写-size 根据文件大小查找 (+ 大于 -小于)(-a并且 -o或者)-us ...

  8. linux下安装TensorFlow(centos)

    一.python安装 centos自带python2.7.5,这一步可以省略掉. 二.python-pip pip--python index package,累世linux的yum,安装管理pyth ...

  9. 从[NOI2008志愿者招募]浅谈线性规划在网络流构图上的巧用

    首先来看一下题..http://www.lydsy.com/JudgeOnline/problem.php?id=1061 1061: [Noi2008]志愿者招募 Description 申奥成功后 ...

  10. 我的Linux系统的VIMRC

    " llvm CODING GUIDELines conformance for VIM" $Revision$"" Maintainer: The LLVM ...