A table has been created using data migration, and data already exists in the table. how can I add, modify, or delete a field? A table has been created using data migration, and data already exists in the table. how can I add, modify, or delete a field?
Reply content:
A table has been created using data migration, and data already exists in the table. how can I add, modify, or delete a field?
Create a new migration file. generally, we create two types of migration files: create a table and modify a table. for example, if you want to create a table, for example, you want to createusers
Table, you will write:
php artisan make:migration create_users_table --create=users
If you have already executedphp artisan migrate
And some data has been inserted. if you want to modify, add, or delete fields, you need to create a new migration file. for example, you need to addemail
Field
php artisan make:migration add_email_column_to_users_table --table=users
Write the content you want in the add_email_column_to_users_table file, and then execute php artisan migrate
As for the migration file, the content is written in the same way. it is clearly written in this document. you can also take a look at my tutorial:
Http://www.zhoujiping.com/scratch/fetching-data.html
In addition, you can checkmigraitons
The record in the table, you should be able to figure out the cause of your previous error
Add Field
Schema::table('users', function ($table) { $table->string('email');});
Modify field
Schema::table('users', function ($table) { $table->string('name', 50)->change();});
Rename a field
Schema::table('users', function ($table) { $table->renameColumn('from', 'to');});
Remove field
Schema::table('users', function ($table) { $table->dropColumn('votes');});