【问题标题】:How to add new column after a custom column existing at the DB [duplicate]如何在数据库中存在的自定义列之后添加新列[重复]
【发布时间】:2021-09-12 05:18:01
【问题描述】:

我想在自定义列之后添加带有迁移的新列,例如我有这个表结构:

Table Screenshot

现在我需要在prd_description 之后添加一个名为badge 的新列(第14 列)。

所以我跑了php artisan make:migration add_badge_to_products_table --table=products

就这样:

public function up()
    {
        Schema::table('products', function (Blueprint $table) {
            $table->tinyInteger('badge')->unsigned()->nullable();
        });
    }

但现在的问题是,我不知道如何在该特定列之后添加该列。因为默认情况下,Laravel 会在表格末尾添加这个。

那该怎么做呢?

【问题讨论】:

    标签: php laravel migration laravel-8 laravel-migrations


    【解决方案1】:

    你需要使用下面的 after 方法

    public function up()
    {
        Schema::table('products', function (Blueprint $table) {
            $table->tinyInteger('badge')->unsigned()->nullable()->after('prd_description');
        });
    }
    

    【讨论】:

      【解决方案2】:

      在单个特定列之后添加列喜欢,

        Schema::table('products', function (Blueprint $table) {
              $table->tinyInteger('badge')->after('prd_description')->unsigned()->nullable();
          });
      

      还添加多列像这样, https://laravel.com/docs/8.x/migrations#column-order

      【讨论】:

        猜你喜欢
        • 2020-09-19
        • 2017-12-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-04
        • 2011-12-17
        • 1970-01-01
        相关资源
        最近更新 更多