Create a post and user model
PHP artisan make:model postphp artisanmake: Model User
Creating Posts and Users table files
PHP artisan make:migration create_users_table--create=usersphp artisanMake: Migration create_users_table-- Create=posts
Set table structure in table file
Schema::create (' posts ',function(Blueprint$table) { $table->increments (' id '); $table-string(' Name '); $table-timestamps (); }); Schema:: Create (' users ',function(Blueprint$table) { $table->increments (' id '); $table-string(' Name '); $table-string(' Email '); $table-string(' Password '); $table-string(' Remember_token '); $table-timestamps (); });
Generate posts and users and notifications tables
PHP Artisan Notifications:tablephp artisan Migrate
Create test data
Set the required data type in database/factories/modelfactoy.php first
$factory-Define(App\user::class,function(Faker\generator$faker) { Static $password; return [ ' Name ' + =$faker->name, ' email ' =$faker->unique ()->safeemail, ' password ' =$password?:$password= Bcrypt (' secret '), ' remember_token ' = Str_random (10), ];});$factory-Define(App\post::class,function(Faker\generator$faker) { return [ ' Name ' + =$faker->name, ];});
Execute the command again.
php artisan tinkernamespace appfactory (' App\user ', Ten)->create ()
Factory (' App\post ', Ten)->create ()
Create the notifications directory, as well as the notification file
Invoicepaid.php and usersubscrible.php files can be seen after creation
PHP artisan make:notification invoicepaidphp artisanmake: Notification usersubscrible
Formatting Database Notifications
In notification class, you can use the toDatabase
ortoArray 方法 , 将数据存入到数据库中 ,,同时 这两个方法接受$notifiable
entity,并返回一个普通的数组(json 格式)。我的代码如下:
//Invoicpaid Public functionToArray ($notifiable) { return [ ' post_id ' =$this->id, ]; }///Usersubscribe Public functionToArray ($notifiable) { return [ ' Subscribe_at ' =>carbon::now (),//Recording Time ]; }
设置路由
Auth::loginusingid (2); Routefunction () { // return view (' Welcome '); Auth:: User ()->notify (new \app\notifications\postpublised ()); Auth:: User ()->notify (new \app\notifications\usersubscribed ());});
When you refresh, you see the data inserted in the database, and the Read_at field is null
Notification data show
Add the following code to the welcome.php to display the data in the form of a hump:
@foreach(auth::user ()->unreadnotifications As $notification) {{--@include(' notification. '). Snake_case (Class_basename ($notification->type)))--}} <li>{{snake_case ( Class_basename ($notification->type))}}</li> @endforeach </ul>
<form method= "POST" action= "/user/notification" >
{{Csrf_field ()}}
<button type= "Submit" > Submit </button>
</form>
Create a new/user/notification route, change the unread notification to read, and modify the value of the Read_at field, the second refresh page will not have data display, and can use this corresponding different users to load different templates
\illuminate\support\facades\route::p ost ('/user/notification ',function () { \illuminate\support\ Facades\auth:: User ()->unreadnotifications->markasread ();});
Database notifications in Laravel