laravel - Laravel - 使用 php artisan make:migration 一次将两列添加到现有表中
问题描述
我是新手laravel
,我想知道我们不能使用将两列添加到现有表中吗
php artisan make:migration
立即为 Ex。如果我的用户表包含id
,user_name
现在我想添加两个新列,如 asuser_phone
和user_email
in one
php artisan make:migration add_user_phone_to_users_table add_user_email_to_users_table
类似的东西?非常抱歉,如果我的问题是错误的。我可以将新字段一一添加到两个单独的迁移中,但想知道是否可以一次将两个新列添加到现有表中。在此先感谢,我希望我能得到满意的答复。
解决方案
创建一个新的迁移是对的,php artisan make:migration add_email_and_phone_number_to_users --table=users
在迁移中,您可以为此添加代码:
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->string('email')->nullable();
$table->string('phone_number')->nullable();
});
}
public function down()
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn(['email', 'phone_number']);
});
}
推荐阅读
- android - 两端带有小尖端的编辑框
- couchdb - CouchDB / PouchDB 中的视图与过滤器
- javascript - 使用js获取开始日期和结束日期之间的周数
- php - 致命错误:未捕获的错误:在字符串中调用成员函数 fetch_row() (while ($sql->fetch_row()) { )
- angular - 组件角度中加载 styleUrls 的条件
- java - 当我尝试从第三方 jar 访问私有方法时观察到 NoSuchMethod 异常
- ios - 在 iOS 12.0 上加载 MapKit 地图时,所有标注都显示而不是隐藏
- php - PHP 响应头内容类型一直是 text/html
- ionic-framework - 如何在 ionic 中获取 IOS 中可用的 WiFi 列表?
- javascript - 无法动态地使内容可编辑以在 angular2+ 中进行内联编辑