author: Kaka
wechat: fangkangfk
The so-called migration like version control database, this mechanism allows the team to easily edit and share simple database table structure applications. Laravel migration usually junctions of the schema constructs can be readily applied to build the database table structure. If you've ever told team members frequently need to manually add columns to the local database table structure to maintain local development environment, then this is the database migration is committed to solve the problem.
In this written document it is not very clear, because only said how to create a data structure, only to modify the method of the column, but did not mention how to modify, then these processes take a look at the following, more still need to study the documents
Create a migration file: php artisan make: migration create_user_table.php
Use the command: php artisan migtate for database migration
The wrong solution:
And then execute on ok
Open the database:
This is the first unified database structure, but in our normal project development project structure will change frequently, this time we can not directly change the direct line, but we also need to maintain a unified database, we can use the data laravel migration to operate
Before you modify a column, make sure that has to doctrine/dbal
depend added to the composer.json
current state of the file, Doctrine DBAL library used to determine the columns and columns to create the required adjustments specified SQL statement:
composer require doctrine/dbal
For example, we have this database users to add a field myIsAdd this field
We need to create a migration file
Then use php artisan migrate command
Database, has been added successfully
Here on the method of operation of the column I briefly listed, just know that this is to modify the structure of the table, the other can see document solutions (such as adding the index of the column, type)
This is to rename the field
Schema::table('users', function (Blueprint $table) {
$table->renameColumn('from', 'to');
});
This is to remove the field
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('votes');
});
There are other special needs can check documents to solve their own, not to list here come