Database specifications in laravel, laraveldatabase

Source: Internet
Author: User

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 usetoDatabaseOrThe toArray method stores data in the database, and both methods accept$notifiableEntity, 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 ();});

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.