建立通知
php artisan make:notification NoticeNotification
Laravel支持通过多种传输通道发送通知,这些通道包括邮件、短信(通过 Nexmo)以及 Slack 等
我这里使用的数据库通知
将via($votifiable)中的买了改为database
public function via($notifiable)
{
return ['database'];
}
新建通知表,当然也可以不用
php artisan make:migration create_notices_table
public function up()
{
Schema::create('notices', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedInteger('from_user_id')->index()->default(0)->comment('用户');
$table->unsignedInteger('to_user_id')->index()->default(0)->comment('用户');
$table->string('notice_type', 10)->comment('类型:1:system');
$table->string('title')->comment("标题");
$table->string('link')->nullable()->comment("链接地址");
$table->text("content")->comment("内容");
$table->timestamps();
});
}
运行通知迁移
php artisan notifications:table
会建立一个迁移文件
Schema::create('notifications', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->string('type');
$table->morphs('notifiable');
$table->text('data');
$table->timestamp('read_at')->nullable();
$table->timestamps();
});
运行迁移
php artisan migrate
再user模型中加入
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use Notifiable;
}
notifications表中的notifiable中将储存通知人,这里即用户id,data表中存储notifiable实体,这个在通知里的toDatabase中定义
public function toDatabase($notifiable)
{
// 将会存入 notifications 表的 data 字段中
return [
'invoice_id' => $this->notice->id, // 通知ID
'user_id' => $this->notice->to_user_id,// 通知人
'notice_type' => $this->notice->notice_type,// 通知类型
'title' => $this->notice->title,// 通知标题
'content' => $this->notice->content,// 通知内容
'link' => $this->notice->link,// 链接地址
];
}
}
通知类
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use App\Model\Admin\Notice;
class NoticeNotification extends Notification
{
use Queueable;
private $notice;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct(Notice $notice)
{
$this->notice = $notice;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['database'];
}
/**
* 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 toDatabase($notifiable)
{
// 将会存入 notifications 表的 data 字段中
return [
'invoice_id' => $this->notice->id, // 通知ID
'user_id' => $this->notice->to_user_id,// 通知人
'notice_type' => $this->notice->notice_type,// 通知类型
'title' => $this->notice->title,// 通知标题
'content' => $this->notice->content,// 通知内容
'link' => $this->notice->link,// 链接地址
];
}
}
使用
存放通知
$userId = Auth::id();
$order = OrderRepository::activeFind($id);
$noticeData = [
'from_user_id' => $userId,
'to_user_id' => $order->user_id,
'notice_type' => 'system',
'title' => __('front.order_notice'),
'content' => __('front.order_notice_content'),
'link' => '/member/order/' . $id
];
$noticeRes = NoticeRepository::add($noticeData);
$noticeRes->tuser->notify(new NoticeNotification($noticeRes));
tuser表示接收人
public function tuser()
{
return $this->belongsTo('App\Model\Admin\User', 'to_user_id');
}
访问通知
访问全部
$user = App\User::find(1);
foreach ($user->notifications as $notification) {
echo $notification->type;
}
访问未读
$user = App\User::find(1);
foreach ($user->unreadNotifications as $notification) {
echo $notification->type;
}
我这里由于使用了单独的表存放通知,方便分页,所以全部通知从通知表里读取的,未读通知才是通过unreadNotifications读取
public function index(Request $request)
{
$userId = Auth::id();
$user = UserRepository::find($userId);
$status = $request->get('status', 0);
$noticeCount = count($user->unreadNotifications);
$list = [];
if ($status == 1) {
foreach ($user->unreadNotifications as $notification) {
$data['id'] = $notification->id;
$data['user_id'] = $notification->data['user_id'];
$data['title'] = $notification->data['title'];
$data['content'] = $notification->data['content'];
$data['link'] = $notification->data['link'];
$data['notice_type'] = $notification->data['notice_type'];
$data['created_at'] = $notification->created_at;
$data['read_at'] = $notification->read_at;
$list[] = $data;
}
} else {
$list = NoticeRepository::activeListByUser(10, $userId);
}
}
设置为已读
$user = App\User::find(1);
foreach ($user->unreadNotifications as $notification) {
$notification->markAsRead();
}
或者
$user->unreadNotifications->markAsRead();
由于我这里需要单个设置已读,所以在循环里做了一个判断
public function save(Request $request)
{
$id = $request->get('id');
if (!empty($id)) {
$userId = Auth::id();
$user = UserRepository::find($userId);
foreach ($user->unreadNotifications as $notification) {
if ($notification->id == $id) {
$notification->markAsRead();
}
}
}
}