[当使用php artisan迁移时,在迁移中更改表名,使用修补程序保存对象时发生错误

问题描述 投票:0回答:1

这是我的迁移,我正在使用mysql,运行迁移时成功了,然后查看我的mysql DB,该表的名称是我想要的“ receivedDocument”。

class CreateReceivedDocumentsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('received_documents', function (Blueprint $table) {
            $table->id();
            $table->dateTime('receivedDate')->nullable();
            $table->string('subjectDescription')->nullable();
            $table->string('fromEntity')->nullable();
            $table->string('documentNumber')->nullable();
            $table->integer('documentTypeId')->nullable();
            $table->dateTime('documentDate')->nullable();
            $table->integer('responseTypeId')->nullable();
            $table->text('remarks')->nullable();
            $table->timestamps();
        });

        Schema::rename('received_documents', 'receivedDocument');
    }

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

并且当我使用修补程序创建和保存记录时,它失败。 Tinker似乎不知道我在迁移过程中更改了表名。

>>> $rd = new \App\ReceivedDocument();
=> App\ReceivedDocument {#3036}
>>> $rd->subjectDescription = 'description sample';
=> "description sample"
>>> $rd->documentNumber = '1234/2020';
=> "1234/2020"
>>> $rd->save();
Illuminate/Database/QueryException with message 'SQLSTATE[42S02]: Base table or
view not found: 1146 Table 'dossier.received_documents' doesn't exist (SQL: inse
rt into `received_documents` (`subjectDescription`, `documentNumber`, `updated_a
t`, `created_at`) values (description sample, 1234/2020, 2020-06-11 10:18:00, 20
20-06-11 10:18:00))'

我如何告诉修补匠我的迁移有什么变化?

php artisan migrate tinker
1个回答
0
投票

在您的模型中,定义表如下:

protected $table = 'my_flights';
© www.soinside.com 2019 - 2024. All rights reserved.