Laravel 5 Framework Learning Database Migration (migrations) _php Example

Source: Internet
Author: User
Tags rollback

Database migrations is one of the most powerful features of Laravel. Database migration can be understood as the version controller of the database.

The Database/migrations directory contains two migration files, one to create a user table and one for user password resets.

In the migration file, the up method is used to create the data table, and the down method is used for rollback, that is, to delete the data table.

Performing a Database migration

Copy Code code as follows:

PHP Artisan Migrate
#输出
Migration table created successfully.
Migrated:2014_10_12_000000_create_users_table
Migrated:2014_10_12_100000_create_password_resets_table

Looking at the MySQL database, you can see that three tables have been generated. The Migratoins table is the Migration log table, users, and Pasword_resets.

Perform a database rollback if there is a problem with the design

Copy Code code as follows:

PHP Artisan Migrate:rollback
#输出
Rolled back:2014_10_12_100000_create_password_resets_table
Rolled back:2014_10_12_000000_create_users_table

Looking at the MySQL database again, the Migrations table is left, and the users password_resets is deleted.

Modify the migration file to perform the migration again.

New migration

Copy Code code as follows:

PHP artisan make:migration create_article_table--create= ' articles '
#输出
Created migration:2015_03_28_050138_create_article_table

A new file was generated under Database/migrations.

<?php use

illuminate\database\schema\blueprint;
Use illuminate\database\migrations\migration;

Class Createarticletable extends Migration {

 /**
 * Run the migrations.
 *
 * @return void */public
 function up ()
 {
 schema::create (' articles '), function (Blueprint $ Table)
 {
  $table->increments (' id ');
  $table->timestamps ();}
 );

 /**
 * Reverse the migrations.
 *
 * @return void
 *
 /Public Function down ()
 {
 Schema::d rop (' articles ');
 }

}

Automatically adds the ID column, automatically grows, timestamps () automatically generates CREATED_AT and Updated_at two time columns. We add some fields:

 Public function up ()
 {
 schema::create (' articles '), function (Blueprint $table)
 {
  $table-> Increments (' id ');
      $table->string (' title ');
      $table->text (' body ');
      $table->timestamp (' published_at ');
  $table->timestamps ();}
 );
 

To perform a migration:

Copy Code code as follows:

PHP Artisan Migrate

Now we have a new datasheet.

Suppose we need to add a new field, you can roll back, then modify the migrated file, perform the migration again, or you can create a new migration file directly

Copy Code code as follows:

PHP Artisan make:migration add_excerpt_to_articels_table

View the newly generated migration files

<?php use

illuminate\database\schema\blueprint;
Use illuminate\database\migrations\migration;

Class Addexcerpttoarticelstable extends Migration {

 /**
 * Run the migrations.
 *
 * @return void
 *
 /Public Function up ()
 {
 //
 }

 /**
 * Reverse the migrations.
 *
 * @return void
 *
 /Public Function down ()
 {
 //
 }

}

Only the empty up and down methods. We can add code manually, or we let laravel generate the underlying code for us. Delete this file, regenerate the migrated file, and note add parameters:

Copy Code code as follows:

PHP artisan make:migration add_excerpt_to_articels_table--table= ' articles '

Now, the up method has the initial code.

 Public function up ()
 {
 schema::table (' articles ', function (Blueprint $table)
 {
  //
 });
 }

Add the actual data modification code:

 Public function up ()
 {
 schema::table (' articles '), function (Blueprint $table)
 {
  $table->text ( ' Excerpt ')->nullable ()
 
 ;} Public function down ()
 {
 schema::table (' articles '), function (Blueprint $table)
 {
  $table-> Dropcolumn (' excerpt ');}
 );
 

Nullable () indicates that the field can also be empty.

Perform the migration again and check the database.

If we do it for fun, roll back

Copy Code code as follows:

PHP Artisan Migrate:rollback

The excerpt column is gone.

The above mentioned is the whole content of this article, hope to be able to give everybody skilled master Laravel5 frame to have help.

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.