Home  >  Article  >  Backend Development  >  laravel data migration problem

laravel data migration problem

WBOY
WBOYOriginal
2016-12-01 00:25:441039browse

I have created a table using data migration. There is already data in the table. Now I want to add fields, modify fields, or delete fields. What should I do?

Reply content:

I have created a table using data migration. There is already data in the table. Now I want to add fields, modify fields, or delete fields. What should I do?

Re-create the migration file. Usually there are two types of migration files we create, one is to create a table, and the other is to modify a table. For example, if you want to create a table, for example, if you want to create the users table, you will Write this:

<code class="bash">php artisan make:migration create_users_table --create=users</code>

If you have executed php artisan migrate, and have inserted some data, and if you want to modify, add or delete fields, then you need to re-create a migration file. For example, you now want to add Each emailfield

<code class="bash">php artisan make:migration add_email_column_to_users_table --table=users</code>

Write what you want in the add_email_column_to_users_table file, and then execute php artisan migrate

As for the writing of the content in the migration file, it is written very clearly in the document, or you can also read my tutorial:

http://www.zhoujiping.com/scratch/fetching-data.html

In addition, if you look at the records in the migraitons table in the database, you should be able to figure out the reason for your previous error

Add fields

<code>Schema::table('users', function ($table) {
    $table->string('email');
});</code>

Modify fields

<code>Schema::table('users', function ($table) {
    $table->string('name', 50)->change();
});</code>

Rename fields

<code>Schema::table('users', function ($table) {
    $table->renameColumn('from', 'to');
});</code>

Remove fields

<code>Schema::table('users', function ($table) {
    $table->dropColumn('votes');
});</code>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn