Php Laravel 8-更改现有迁移

Php Laravel 8-更改现有迁移,php,laravel,Php,Laravel,我正在Laravel Framework 8.33.1上开发,并在本地环境和生产环境中进行了以下迁移 class CreateCompanyTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('company', function (Blueprint

我正在
Laravel Framework 8.33.1
上开发,并在本地环境和生产环境中进行了以下迁移

class CreateCompanyTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('company', function (Blueprint $table) {
            $table->id();
            $table->integer('company_id');
            $table->integer('messageId');
            $table->string('url');
            $table->timestamps();

            /**
            New table:
            $table->id();
            $table->integer('company_id')->nullable($value = true);
            $table->integer('messageId');
            $table->integer('people_id')->nullable($value = true);
            $table->string('url')->nullable($value = true);
            $table->timestamps();
             */
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('company');
    }
}

我想使用以下调整字段更改迁移:

            $table->id();
            $table->integer('company_id')->nullable($value = true);
            $table->integer('messageId');
            $table->integer('people_id')->nullable($value = true);
            $table->string('url')->nullable($value = true);
            $table->timestamps();
由于我在生产中使用当前的迁移,我不想丢失数据

我只是尝试用我的新表定义修改迁移文件,但我得到:

> php artisan migrate
Nothing to migrate.

有没有关于如何在laravel中正确更改迁移的建议


谢谢你的回复

要修改现有表,请创建新的迁移

php artisan make:migration alter_company_表

类AlterCompanyTable扩展了迁移
{
/**
*运行迁移。
*
*@返回无效
*/
公共职能
{
架构::表('company',函数(Blueprint$表){
$table->integer('company_id')->nullable()->change();
$table->integer('people_id')->nullable();
$table->string('url')->nullable()->change();
});
}
/**
*反向迁移。
*
*@返回无效
*/
公共职能部门
{
架构::表('company',函数(Blueprint$表){
$table->integer('company_id')->nullable(false)->change();
$table->dropColumn('people_id');
$table->string('url')->nullable(false)->change();
});
}
}

如果您不介意丢失数据并且处于开发模式,您可以执行
php artisan migrate:rollback
,执行后执行:
php artisan migrate
。因此,您的迁移工作正常。你可以阅读更多

现在,如果您只想添加新表并修改数据库中的其他表,那么应该进行另一次迁移。但在此之前,您应该先安装,以便能够正确修改表,否则,它会给您带来许多错误。之后,在
config/database.php

use Illuminate\Database\DBAL\TimestampType;

'dbal' => [
    'types' => [
        'timestamp' => TimestampType::class,
    ],
],
现在,您可以执行以下操作:
php artisan make:migration将新表添加到公司
,并在
数据库/migrations
中创建文件

class AlterCompanyTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('company', function (Blueprint $table) {
            $table->integer('company_id')->nullable()->change();
            $table->integer('people_id')->nullable();
            $table->string('url')->nullable()->change();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('company', function (Blueprint $table) {
            $table->integer('company_id')->nullable(false)->change();
            $table->dropColumn('people_id');
            $table->string('url')->nullable(false)->change();
        });
    }
}

如果公司桌子掉了,可以吗?这几乎是你所需要的一切。