Laravel Database Migration (DB migration) Operations instance

Source: Internet
Author: User
Tags composer install version control system

Many people may be learning the Laravel framework, the Laravel Database Migration (hereinafter referred to as migration) There is doubt: what is migration. Why to use migration. Migration in the end convenient where.

Well, with these questions, we will come together today to study migration.

Question 1: What is migration.

Answer: You can view migration as a type of VCs (version control System) of a database, or versioning systems. You can use the Laravel Artisan command to quickly create or restore migration files for database architecture.

Question 2: Why use migration.

Answer: Using migration can effectively version control the database, and follow the single responsibility principle (one responsibility principle), more convenient database control.

The question 3:migration in the end conveniently where.

Basically do not need to write SQL code will be able to quickly and easily build database structure, and can quickly migrate or rollback, realize the database version control and management.

To use migration, we can use the Artisan command to create a migration file:

# First enter the root of our Laravel project
$ cd my-laravel-project

# Enter the following artisan command
$ php artisan make:migration Create_samples_ Table--create=samples

Note--create==samples This option will tell artisan that we are going to create a new table named samples, so artisan will automatically create a name 2017_03_13_ in the Database\migrations directory 061422_create_samples_table.php files (where the prefix is the date and time the file was created, so each person will be slightly different), and automatically populate the Schema::create method to make it easier for us to create more column:

As you can see, Laravel has added three columns for us, one for IDs and two for Created_at and updated_at (type DateTime) created by $table->timestamps ().

Laravel default assumes that each table will have an ID column, and that each table must have Created_at and updated_at two column.

What would we do if we were to add a new varchar type column called My_sample_code in the samples.

It's simple, just add this line:

$table->string (' My_sample_code ');

If we want to limit the length of the varchar, we can add it in the second argument:

$table->string (' My_sample_code ', 100);

So now that we have the migration file, how can we build our samplestable in the database?

Just run a simple artisan command in our laravel root directory:

$ PHP Artisan Migrate

voila! Done.
Now we are in the database, we can see a table called the samples.

So if we want to make a change to this table or add another column.
Then we have two options:
The first, if you just set up a table, and there is no data, you can choose rollback or reset, that is, use

$ PHP Artisan Migrate:rollback

Or

$ PHP Artisan Migrate:refresh

It is worth noting that the default option for rollback this command will only revert to the previous migration, and refresh will reset all migration, equivalent

$ PHP artisan migrate:reset
$ php Artisan Migrate

Where the reset command will rollback all migration to the initial stage.

The second method is to create a migration file again. But we need to add a dependency first. Open the Composer.json file under the root of the project and add Doctrine/dbal to this package in the Require column:

    "Require": {"
        php": ">=5.6.4",
        "laravel/framework": "5.4.*",
        "Laravel/tinker": "~1.0",
        " Doctrine/dbal ":" 2.5.0 "
    },

Then run composer install this command.

Well, if we want to limit the column length of the My_sample_code we added earlier to 200, we can create a new migration file:

$ php Artisan make:migration modify_samples_table--table=samples

Where we fill in the following code in the Schema::table function:

$table->string (' My_sample_code ')->change ();

And then run migrate again, that's that simple:

$ PHP Artisan Migrate

So if we want to change this column to another type. For example, we want to change it to the text type, so what do we need to do?

$table->text (' My_sample_code ')->change ();

Run migrate again.

As you can see, my_sample_code this column has become the text type.

These are the basic techniques for Laravel database migration. There are questions welcome to discuss together.

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.