Database specifications in laravel, laraveldatabase
Create a Post and User model
php artisan make:model Postphp artisan make:model User
Create a posts and users table File
php artisan make:migration create_users_table --create=usersphp artisan make:migration create_users_table --create=posts
Set the table structure in the 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 the posts and users and notifications table
php artisan notifications:tablephp artisan migrate
Create Test Data
Set the required data type in database/factories/ModelFactoy. php.
$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, ];});
Then execute the command
php artisan tinkernamespace Appfactory('App\User',10)->create()
factory('App\Post',10)->create()
Create the Notifications directory and notification files
The InvoicePaid. php and UserSubscrible. php file is displayed after creation.
php artisan make:notification InvoicePaidphp artisan make:notification UserSubscrible
Formatting Database specifications
You can usetoDatabase
OrThe toArray method stores data in the database, and both methods accept$notifiable
Entity, and returns a common array (in json format ). My code is as follows:
// InvoicPaid public function toArray ($ notifiable) {return ['Post _ id' =>$ this-> id,] ;}// UserSubscribepublic function toArray ($ notifiable) {return ['subscribe _ at' => Carbon: now (), // record time];}
Set route
Auth::LoginUsingId(2);Route::get('/', function () { // return view('welcome'); Auth::user()->notify(new \App\Notifications\PostPublised()); Auth::user()->notify(new \App\Notifications\UserSubscribed());});
When refreshing, you can see that the data is inserted in the database, and the read_at field is null.
Notification Data Display
Add the following code in welcome. php to display data in the form of a hump:
<H2> unread notification
<Form method = "post" action = "/user/notification">
{Csrf_field ()}}
<Button type = "submit"> submit </button>
</Form>
\ Illuminate \ Support \ Facades \ Route: post ('/user/notification', function () {\ Illuminate \ Support \ Facades \ Auth: user () -> unreadNotifications-> markAsRead ();});