Laravel 5.2 Database Migrations and data population

Source: Internet
Author: User

First, the Database migration

Laravel's Database migration provides a series of related operations on databases, tables, fields, and indexes.

1. Create a migration

Use the Artisan command php Artisan make:migration create_links_table

This will generate a friendly link migration class named 2017_05_06_151645_create_links_table.php in the Database/migrations directory. Where the first half of the name "2017_05_06_151645_" is the timestamp of the Laravel increment.

2. Writing logic

Write code to manipulate the data table for the UP () and down () methods of the migration class.

2017_05_06_151645_create_links_table.php File Contents:

<?PHP UseIlluminate\database\schema\blueprint; Useilluminate\database\migrations\migration;classCreatelinkstableextendsmigration{/** * Perform migration * * @return void*/     Public functionUp () {Schema:: Create (' Links ',function(Blueprint$table){            $table->engine = ' MyISAM '; $table->increments (' id '); $table-string(' name ')default(')->comment (' name ')); $table-string(' title ')default(')->comment (' title ')); $table-string(' URL ')default(')->comment (' address ')); $table-integer(' sort ')default(->comment) (' sort '));    }); }        /** * Rollback Migration * * @return void*/     Public functionDown () {Schema::d rop (' links '); }}2017_05_06_151645_create_links_table. php
3. Performing the migration

Use the Artisan command PHP Artisan migrate

Now, a hd_links table is created in the database and a table hd_migrations ("Hd_" is the configured table prefix) that records the migration:

Note: If you manually delete the migrated class file, use the composer Dump-autoload command to optimize the auto-load to re-make:migration.

Second, data filling

Can be used for testing to populate tables in a database with some data.

1. Create a fill

Use the Artisan command php Artisan make:seeder linkstableseeder

This will generate a friendly link fill class named linkstableseeder.php in the Database/seeds directory.

2. Writing logic

linkstableseeder.php File Contents:

<?PHP UseIlluminate\database\seeder;classLinkstableseederextendsseeder{/** * Run Database population * * @return void*/     Public functionrun () {$data= [            [                ' Name ' = ' Laravel Chinese Community ', ' title ' + ' Laravel China Community-high quality Laravel and PHP developer community-Powered by Phphub ', ' url ' = ' https://laravel-china.org/', ' sort ' = ' 49 '            ],            [                ' Name ' = ' GitHub ', ' title ' = ' GitHub ' is where people build software. More than million people use ... ', ' url ' = ' https://github.com ', ' sort ' = ' 49 '            ]        ]; DB:: Table (' Links ')->insert ($data); }}
3. Call the Fill

In the Database/seeds directory databaseseeder.php This database population class, the fill is called within the Run () method.

databaseseeder.php File Contents:

<? PHP  Use Illuminate\database\seeder; class extends seeder{    /* * *     Run database population     *     * @return     void    */ publicfunction  run ()    {        $this->call (linkstableseeder::  Class);}    }
4. Perform the Fill

Use the Artisan command PHP Artisan db:seed

Now, the Hd_links table in the database has 2 records:

Laravel 5.2 Database migration and data population

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.