Laravel Database Migration (iii)

Source: Internet
Author: User

Laravel claims to be the best frame in the world, the database migration is counted as a, here is the first simple door:

Laravel is very powerful, it writes the operations in the table as a migrations migration file,
You can then manipulate the table directly by migrating the files.
So, the data migration file is the statement file of the statement File action table of the action table
Why use a migration file instead of just knocking at the SQL action table?
1. Facilitate the team to unify the Operation table.
2. Problems, easy to track down problems and backtracking, there is a historical fallback function.

Create a library First:

Configure the file, modify the. Env parameters

Cmd.exe command line enter the following command to create a migration file for a table: PHP artisan make:migration Create_table_liuyan--create=liuyan

Not busy. This understands the meaning of this command, and looks at a file generated after executing this command:

Open this file, which is the PHP file:

<?PHP UseIlluminate\database\schema\blueprint; Useilluminate\database\migrations\migration;classCreatetableliuyanextendsmigration{/** * Run the migrations. * * @return void*/     Public functionUp () {Schema:: Create (' Liuyan ',function(Blueprint$table) {            $table->increments (' id '); $table-timestamps ();    }); }    /** * Reverse the migrations. * * @return void*/     Public functionDown () {Schema::d rop (' Liuyan '); }}

OK, compare and look at the previous command, explain: The Php.exe interpreter creates a migration (migration) file by artisan, where the class name Createtableliuyan corresponds to Create_table_liuyan ( Whatever you like, you're happy, but still follow the rules, and finally--create=liuyan is a fixed format that creates a Liuyan table.

We're going to modify this migrated file.

<?PHP UseIlluminate\database\schema\blueprint; Useilluminate\database\migrations\migration;classCreatetableliuyanextendsmigration{/** * Run the migrations. * * @return void*/     Public functionUp () {Schema:: Create (' Liuyan ',function(Blueprint$table) {            $table->increments (' id '); $table->char (' username ', 10); $table->char (' Password ', 50); $table->tinyinteger (' Sex '); $table->char (' title ', 20); $table-string(' content ', 200); $table->tinyinteger (' Pubtime '); $table->tinyinteger (' IP ');    }); }    /** * Reverse the migrations. * * @return void*/     Public functionDown () {Schema::d rop (' Liuyan '); }}

OK, execute command in cmd.exe window: PHP artisan Migrate

OK, create the Migration table successfully, use another CMD window to see if there is a table under WMSGS:

OK, with the Liuyan table, and the other three tables are migration auto-generated tables

Similarly, if we want to add one of the table columns, for example, we want to add a table column email, enter the following command:

PHP Artisan make:migration Add_email_to_liuyan--table=liuyan

Generate a table column migration file, which is also a PHP file:

Open this file:

<?PHP UseIlluminate\database\schema\blueprint; Useilluminate\database\migrations\migration;classAddemailtoliuyanextendsmigration{/** * Run the migrations. * * @return void*/     Public functionUp () {Schema:: Table (' Liuyan ',function(Blueprint$table) {            //        }); }    /** * Reverse the migrations. * * @return void*/     Public functionDown () {Schema:: Table (' Liuyan ',function(Blueprint$table) {            //        }); }}

Change the generated PHP migration file to read as follows:

<?PHP UseIlluminate\database\schema\blueprint; Useilluminate\database\migrations\migration;classAddemailtoliuyanextendsmigration{/** * Run the migrations. * * @return void*/     Public functionUp () {Schema:: Table (' Liuyan ',function(Blueprint$table) {            $table-string(' Email ');    }); }    /** * Reverse the migrations. * * @return void*/     Public functionDown () {Schema:: Table (' Liuyan ',function(Blueprint$table) {            $table->dropcloumn (' email ');    }); }}

Execute the command in the Cmd.exe window:

OK, increase the table column success, open another CMD window to look at:

Email successfully added to the Liuyan table!

Congratulations, you're here. Database migration Getting Started

Of course, there are many orders need to go more knock more remember, refueling!

Programmers are forced to use only the command line, no command line out, haha

Laravel Database Migration (iii)

Related Article

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.