laravel的数据库通知频道
创建数据库通知表
php artisan notifications:table
执行数据库迁移
php artisan migrate
users表增加消息字段
php artisan make:migration add_notification_count_to_users_table --table=users
$table->integer('notification_count')->unsigned()->default(0)
$table->dropColumn('notification_count');
php artisan migrate
生成通知类
$ php artisan make:notification TopicReplied
修改配置
<?php
namespace App\Notifications;
use App\Models\Reply;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
class TopicReplied extends Notification
{
use Queueable;
public $reply;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct(Reply $reply)
{
//
$this->reply = $reply;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['database'];
}
public function toDatabase($notifiable)
{
$topic = $this->reply->topic;
$link = $topic->link(['#reply' . $this->reply->id]);
return [
'reply_id' => $this->reply->id,
'reply_content' => $this->reply->content,
'user_id' => $this->reply->user->id,
'user_name' => $this->reply->user->name,
'user_avatar' => $this->reply->user->avatar,
'topic_link' => $link,
'topic_id' => $topic->id,
'topic_title' => $topic->title,
];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
// public function toMail($notifiable)
// {
// return (new MailMessage)
// ->line('The introduction to the notification.')
// ->action('Notification Action', url('/'))
// ->line('Thank you for using our application!');
// }
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
// public function toArray($notifiable)
// {
// return [
// //
// ];
// }
}
触发通知
在观察器中
public function created(Reply $reply)
{
$topic = $reply->topic;
// $reply->topic->increment('reply_count', 1);
$topic->user->notify(new TopicReplied($reply));
}
重写模型Notifiable方法
class User extends Authenticatable
{
use Notifiable {
notify as protected laravelNotify;
}
public function notify($instance)
{
// 如果要通知的人是当前用户,就不必通知了!
if ($this->id == Auth::id()) {
return;
}
$this->increment('notification_count');
$this->laravelNotify($instance);
}
.
.
.
}
生成通知控制器
$ php artisan make:controller NotificationsController
相关文章
- 如何通过xhprof分析性能
使用方法 xhprof_enable(); /** ... 要检查的php代码 ... **/ $xhprof_data = xhprof_disable(); // 引入xhprof_lib i
- LUMEN API Controller 规范
1. 第三方依赖库规范 在使用LUMEN实现API接口时,以下库必须需要包含在composer包依赖中,以实现代码编写的一些规范 dingo/api : 实现API接口库 vlucas/phpdo
- PHP文件锁
共享锁(LOCK_SH) 什么时候加共享锁? 当在读取数据的时候同时进行着其他的写操作,这个时候需要对文件加共享锁,否则无论有没有对写操作加写锁都会写入成功,导致数据不一致 当文件获得共享锁时,其他
- Hello-Risen-程序
首先需要说明的是,您下载到的文件包含两部分,其中src中是开发源码,用于对Risen框架本身的开发,risen 目录中是通过源码生成的包含debug和release版本的框架程序,用于您应用程序的开发
- PHP自定义类示例(Weixin消息解析类)
PHP自定义类示例(Weixin消息解析类) /** * Created by Qingger. * User: jsspf * Date: 2017/3/24 * Time: 10:50
随机推荐
- 如何通过xhprof分析性能
使用方法 xhprof_enable(); /** ... 要检查的php代码 ... **/ $xhprof_data = xhprof_disable(); // 引入xhprof_lib i
- LUMEN API Controller 规范
1. 第三方依赖库规范 在使用LUMEN实现API接口时,以下库必须需要包含在composer包依赖中,以实现代码编写的一些规范 dingo/api : 实现API接口库 vlucas/phpdo
- PHP文件锁
共享锁(LOCK_SH) 什么时候加共享锁? 当在读取数据的时候同时进行着其他的写操作,这个时候需要对文件加共享锁,否则无论有没有对写操作加写锁都会写入成功,导致数据不一致 当文件获得共享锁时,其他
- Hello-Risen-程序
首先需要说明的是,您下载到的文件包含两部分,其中src中是开发源码,用于对Risen框架本身的开发,risen 目录中是通过源码生成的包含debug和release版本的框架程序,用于您应用程序的开发
- PHP自定义类示例(Weixin消息解析类)
PHP自定义类示例(Weixin消息解析类) /** * Created by Qingger. * User: jsspf * Date: 2017/3/24 * Time: 10:50