Laravel迁移表字段的类型更改

编程入门 行业动态 更新时间:2024-10-27 10:25:24
本文介绍了Laravel迁移表字段的类型更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

以下是我的文件 2015_09_14_051851_create_orders_table.php . 我想通过新迁移将$table->integer('category_id');更改为字符串.

Following is my file 2015_09_14_051851_create_orders_table.php. And I want to change $table->integer('category_id'); as a string with new migration.

<?php use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateOrdersTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('orders', function(Blueprint $table) { $table->increments('id'); $table->string('num'); $table->integer('user_id'); $table->text('store_name'); $table->integer('store_name_publication'); $table->string('postal_code', 255); $table->string('phone_number', 255); $table->text('title'); $table->text('description'); $table->string('list_image_filename1', 255); $table->string('list_image_filename2', 255)->nullable(); $table->string('list_image_filename3', 255)->nullable(); $table->string('list_image_filename4', 255)->nullable(); $table->string('list_image_filename5', 255)->nullable(); $table->integer('term'); $table->datetime('state0_at')->nullable(); $table->datetime('state1_at')->nullable(); $table->datetime('state2_at')->nullable(); $table->datetime('state3_at')->nullable(); $table->datetime('state4_at')->nullable(); $table->datetime('state5_at')->nullable(); $table->datetime('state6_at')->nullable(); $table->datetime('state7_at')->nullable(); $table->datetime('state8_at')->nullable(); $table->datetime('state9_at')->nullable(); $table->datetime('state10_at')->nullable(); $table->integer('category_id'); $table->integer('target_customer_sex'); $table->integer('target_customer_age'); $table->integer('payment_order'); $table->integer('num_comment'); $table->integer('num_view'); $table->string('num_pop'); $table->integer('money'); $table->integer('point'); $table->datetime('closed_at'); $table->timestamps(); $table->softDeletes(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::drop('orders'); } }

推荐答案

更新:2018年10月31日,仍可在laravel 5.7上使用 laravel/docs/5.7/migrations#modifying-columns

update: 31 Oct 2018, Still usable on laravel 5.7 laravel/docs/5.7/migrations#modifying-columns

要对现有数据库进行一些更改,可以在迁移中使用change()来修改列类型.

To make some change to existing db, you can modify column type by using change() in migration.

这就是你可以做的

Schema::table('orders', function ($table) { $table->string('category_id')->change(); });

请注意,您需要向composer.json添加 doctrine/dbal 依赖项 有关更多信息,请参见 laravel/docs/5.1/migrations #modifying-columns

please note you need to add doctrine/dbal dependency to composer.json for more information you can find it here laravel/docs/5.1/migrations#modifying-columns

更多推荐

Laravel迁移表字段的类型更改

本文发布于:2023-10-17 22:09:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1502219.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表字   类型   Laravel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!