Laravel5.1 Configuration database and migration

Source: Internet
Author: User

In Laravel we can change our configuration in the. env file of the project root directory.

After opening the. env file, find the DB prefix for several fields to change our configuration, if you want to do more configuration can be modified in \config\database.php.

Once configured, let's initialize the database and use Laravel's own migration to generate the user and password tables:

PHP Artisan Migrate

After executing this command, you will find that the database has more user and password tables so that our database is configured.

Database Migration-- Migrations

Migration is a database migration, can also be called as version control, so that others download your source code with these migration files only need to execute PHP artisan migrate command to generate all the tables, it is convenient.

Create a migration file

The name of the migration file we created should be clear, and later--create=articles declares a articles table to be created.

In \database\migrations\ we can see the migration file we just created:

classCreatearticlestableextendsmigration{/** * Run the migrations. * * @return void*/     Public functionUp () {Schema:: Create (' articles ',function(Blueprint$table) {            $table->increments (' id '); $table-timestamps ();    }); }    /** * Reverse the migrations. * * @return void*/     Public functionDown () {Schema::d rop (' articles '); }}

There are two methods: up and down,up nothing to say, is the field to be loaded when executing the migrate command, down is what to do when the rollback.

Execute command:

PHP Artisan Migrate

After opening the table ... Oh.. Forgot to add the field, at this point we can create such a migration:

PHP Artisan make:migration insert_content_to_articles--table=articles

The--table=articles at this point is that we are going to make some changes to the articles table and edit this migration:

<?PHP UseIlluminate\database\schema\blueprint; Useilluminate\database\migrations\migration;classInsertcontenttoarticlesextendsmigration{/** * Run the migrations. * * @return void*/     Public functionUp () {Schema:: Table (' articles ',function(Blueprint$table) {            //Add the fields we want            $table-string(' content ');    }); }    /** * Reverse the migrations. * * @return void*/     Public functionDown () {Schema:: Table (' articles ',function(Blueprint$table) {            //the corresponding rollback operation            $table->dropcolumn (' content ');    }); }}

Perform a migrate operation to see if the operation was successful.

Rollback operation

Roll back the Last migration:

PHP Artisan Migrate:rollback

Roll back all the migrations:

PHP Artisan Migrate:reset

Laravel5.1 Configuration database and migration

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.