Laravelmigration basics (database)-php Tutorial

Source: Internet
Author: User
Tags laravel migration
Laravelmigration Basic (database) migration is a command line tool with many features, including creating controllers, middleware, and database creation tables.

Process

The whole process is to use the migration tool to create an executable php file, which contains statements for modifying the database, and then execute this executable php file to write it to the database.

Php artisan make: migration create_articles_table -- create = articles

  • -- Create is the table name of the specified database.
  • Create_articles_table is the name of the executable php file generated by migration.
An example of the generated executable file is as follows:

Database/migrations/XXXX. php

Class CreateArticlesTable extends Migration {/*** Run the migrations. ** @ return void */public function up () // up indicates the database creation method {Schema: create ('articles', function (Blueprint $ table) {// create $ table-> increments ('id') is used here; // These are the database field generation methods, for more information, see the https://laravel.com/docs/5.2/migrations#creating-tables $ table-> timestamps ();}/*** Reverse the migrations. ** @ return void */public function down () // Down indicates the method for deleting the database {Schema: drop ('Articles '); // you can see that drop is used here, mainly for rollback or clearing the database. }}
Execute php artisan migration to write data to the database rollback to roll back the database

Php artisan migrate: rollback rolls back the last operation. rollback mainly relies on the down method and a table that records the rollback operation.

Add Field

Php artisan make: migration add_info_column_to_articles -- table = articles

  • -- Table is the name of the table to be modified.

After the command is executed, an executable php file, database/migrations/XXXX. php

Class AddInfoColumnToArticles extends Migration {/*** Run the migrations. ** @ return void */public function up () {Schema: table ('articles', function (Blueprint $ table) {// write the field you need to add here});}/*** Reverse the migrations. ** @ return void */public function down () {Schema: table ('articles', function (Blueprint $ table) {// since the added field is written, next, write the corresponding deletion method for this field, mainly to delete this field in the future });}}

After modification, run the php artisan migration command again to write data to the database.

This article was created by Peter yuan and is licensed on the Chinese mainland using the signature-non-commercial use 2.5. You need to contact the author before reprinting and referencing, and sign the author and indicate the source of the article. Teenagers like God» laravel migration basics (database)

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.