Laravel Database Migration: Seamlessly Manage Database Structure Changes
Overview
During the development process, database structure changes are a common requirement. However, updating the database structure manually can be tedious and errors can occur frequently. To solve this problem, Laravel provides a database migration tool that can help us easily manage changes to the database structure.
What is database migration?
Database migration is a method of managing database structure changes using code. By using migration scripts, we can easily create tables, modify table structures, add, delete, modify fields, and more. The database migration tool will automatically track the details of each change we make and ensure that each change can be correctly applied or rolled back to achieve seamless management of database structure changes.
Basic operations of Laravel database migration
First, we need to create a migration script. You can use the following command to generate a new migration script:
php artisan make:migration create_users_table
This will create a new migration file in the "database/migrations" directory with a file name similar to "2022_01_01_000000_create_users_table.php". Next, we can define our database structure changes in the "up" method of the migration file:
use IlluminateDatabaseMigrationsMigration; use IlluminateDatabaseSchemaBlueprint; use IlluminateSupportFacadesSchema; class CreateUsersTable extends Migration { public function up() { Schema::create('users', function (Blueprint $table) { $table->id(); $table->string('name'); $table->string('email')->unique(); $table->timestamp('email_verified_at')->nullable(); $table->string('password'); $table->rememberToken(); $table->timestamps(); }); } public function down() { Schema::dropIfExists('users'); } }
In this example, we created a "users" table and added some common fields, such as " name", "email" and "password". The "up" method is used to define the operations of adding fields and creating tables, while the "down" method is used to define the rollback operation, that is, undoing the changes we made.
Execute Migration
When we have finished writing the migration script, we can run the following command to execute the migration:
php artisan migrate
This will automatically run all of our migration scripts that have not yet been run, and Apply changes to the database. If we need to roll back to a previous version, we can run the following command:
php artisan migrate:rollback
This will undo the most recent migration operation.
Update migration
During the development process, we often need to modify the existing database structure. When we need to modify an existing table, we can create a new migration script and use the Schema method provided by Laravel in the "up" method to perform the change operation. For example, we want to add an "age" field to the "users" table:
use IlluminateDatabaseMigrationsMigration; use IlluminateDatabaseSchemaBlueprint; use IlluminateSupportFacadesSchema; class AddAgeToUsers extends Migration { public function up() { Schema::table('users', function (Blueprint $table) { $table->integer('age')->default(18); }); } public function down() { Schema::table('users', function (Blueprint $table) { $table->dropColumn('age'); }); } }
In this example, we use the "Schema::table" method to modify an existing table. We added an integer field called "age" and set the default value to 18. If we need to rollback this operation, we can run the "php artisan migrate:rollback" command.
Summary
By using the Laravel database migration tool, we can easily manage changes to the database structure. Migration tools automate the process of database changes, allowing us to focus on development and iteration without having to worry about the consistency of the database structure. I hope this article can help readers better understand and use database migration tools.
The above is the detailed content of Laravel database migration: seamlessly manage database structure changes. For more information, please follow other related articles on the PHP Chinese website!