laravel data migration problem

WBOY
Release: 2023-03-01 17:52:02
Original
1063 people have browsed it

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>
Copy after login

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>
Copy after login

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>
Copy after login

Modify fields

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

Rename fields

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

Remove fields

<code>Schema::table('users', function ($table) {
    $table->dropColumn('votes');
});</code>
Copy after login
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!