Laravel5 framework learning-database migration (Migrations)

Source: Internet
Author: User
This article introduces databasemigrations, one of the most powerful functions in the Laravel5 framework. This article describes in detail the steps and methods of database migration, which is very practical, if you have any need, refer. Database migrations is one of laravel's most powerful functions. Database migration can be understood as the version controller of the database.

The database/migrations directory contains two migration files, one for creating a user table and the other for resetting the user password.

In the migration file, the up method is used to create a data table, and the down method is used to roll back the data table, that is, to delete the data table.

Execute database migration

The code is as follows:


Php artisan migrate
# Output
Migration table created successfully.
Migrated: 2014_10_12_000000_create_users_table
Migrated: 2014_10_12_100000_create_password_resets_table

View the mysql database. three tables are generated. The migratoins table is the migration record table, users and pasword_resets.

If the design is faulty, perform database rollback.

The code is as follows:


Php artisan migrate: rollback
# Output
Rolled back: 2014_10_12_100000_create_password_resets_table
Rolled back: 2014_10_12_000000_create_users_table

Check the mysql database again, and the migrations table is left. the users password_resets is deleted.

Modify the migration file and run the migration again.

New migration

The code is as follows:


Php artisan make: migration create_article_table -- create = 'articles'
# Output
Created Migration: 2015_03_28_050138_create_article_table

Create a new file under database/migrations.

<?phpuse 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::drop('articles'); }}

The id column is automatically added and automatically increases. timestamps () will automatically generate the created_at and updated_at 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(); }); }

Perform migration:

The code is as follows:


Php artisan migrate

Now we have a new data table.

Suppose we need to add a new field, you can roll back, modify the migration file, and execute the migration again, or you can directly create a new migration file.

The code is as follows:


Php artisan make: migration add_excerpt_to_articels_table

View new migration files

<?phpuse 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 up and down methods are empty. You can manually add the code, or let laravel generate the basic code for us. Delete this file and regenerate the migration file. add the following parameters:

The code is as follows:


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

Now, the up method contains 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 be empty.

Execute the migration again and check the database.

If we perform rollback for fun

The code is as follows:


Php artisan migrate: rollback

The excerpt column does not exist.

The above is all the content of this article. I hope it will be helpful for you to master the Laravel5 framework.

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.