laravel 数据迁移问题
使用数据迁移创建了一个表,表里已经有数据了,这个时候想要添加字段,或修改字段,或删除字段,要怎么操作?
回复内容:
使用数据迁移创建了一个表,表里已经有数据了,这个时候想要添加字段,或修改字段,或删除字段,要怎么操作?
重新建立migration文件,通常我们建立的migration文件有两种,一种是创建表,另一种是修改表,比如说你要创建一个表,打个比方你要创建users
表,你会这么写:
php artisan make:migration create_users_table --create=users
如果你已经执行了php artisan migrate
, 并且已经插入了一些数据,这时候你如果想修改,添加或者删除当中的字段,那么你需要重新建立一个migration文件,打个比方,你现在要添加个email
字段
php artisan make:migration add_email_column_to_users_table --table=users
将你要的内容写在add_email_column_to_users_table文件中,然后在执行 php artisan migrate
至于migration文件中的内容的写法都一样,文档中非常清楚的写着,或者你也可以看下我这篇教程:
http://www.zhoujiping.com/scratch/fetching-data.html
另外你看下数据库中的migraitons
表中的记录,你应该会想通你之前出错的原因
添加字段
<code>Schema::table('users', function ($table) { $table->string('email'); });</code>
修改字段
<code>Schema::table('users', function ($table) { $table->string('name', 50)->change(); });</code>
重命名字段
<code>Schema::table('users', function ($table) { $table->renameColumn('from', 'to'); });</code>
移除字段
<code>Schema::table('users', function ($table) { $table->dropColumn('votes'); });</code>

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

适用于 Ubuntu 和 Debian 的 PHP 8.4 安装和升级指南

如何设置 Visual Studio Code (VS Code) 进行 PHP 开发
