这里我们使用 Laravel 开发一个站内信的功能。
文档 - 消息通知
编辑 User 模型
使用 Notifiable Trait<?php
namespace App\Models;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use Notifiable; // 使用 Notifiable Trait
}
这个 trait,它包含着一个可以用来发送通知的方法:notify。
生成数据库
Laravel 支持通过多种频道发送通知,包括邮件、短信 (通过 Nexmo),以及 Slack。通知还能存储到数据库以便后续在 Web 页面中显示。这里我们则使用存储到数据库的方式。php artisan notifications:table
php artisan migrate
生成通知类
Laravel 中的一条通知就是一个类 (通常存放在 app/Notifications 文件夹下)。php artisan make:notification InvoicePaid
然后修改 app/Notifications/InvoicePaid:<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\Mai