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)