使用Notification实现站内信

1 创建notification

php artisan make:notification PostPublished

此时app\Notifications目录中有PostPublished.php文件,打开该文件。可以看到下面的方法内容。

public function via($notifiable)
{
    //站内信
    return ['database'];
}

public function toArray($notifiable)
{
    //该返回内容存放在CreateNotificationsTable.php的data字段中
    return $this->post->toArray();
}

2 执行notifications:table命令

notifications:table Create a migration for the notifications table

php artisan notifications:table

此时会产生一个CreateNotificationsTable.php文件,打开该文件

class CreateNotificationsTable extends Migration
{
    public function up()
    {
        Schema::create('notifications',function(Blueprint $table){
            $table->string('id')->primary();
            //type字段是记录notification是那种类型的
            $table->string('type');
            $table->morphs('notifiable');
            //data是要记录的具体内容(app\Notifications\PostPublished.php文件中的toArray中数据)
            $table->text('data');
            $table->timestamp('read_at')->nullable();
            $table->timestamps();
        });
    }
}

3 生成一个数据库表

php artisan migrate //migrate命令也可以

此时生长一个表create_notifications_table

4 修改路由

Auth::loginUsingId(2);

Route::get('/',function(){
    $post=\App\Post::find(4);

    Auth::user()->notify(new \App\Notifications\PostPublished($post));
});

5 触发路由

在地址栏中输入网页地址,触发路由。

6 链接tinker进行数据测试

链接tinker

php artisan tinker //tinker

数据测试

Auth::user()->notifications
//此时下面会产生一系列数据集

未完待续

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值