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
//此时下面会产生一系列数据集
未完待续