Database notifications in Laravel

Source: Internet
Author: User
Tags auth button type generator

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.