Search code examples
phplaravel-artisanmigratetinker

when using php artisan migrate, change table name in migration, error occur when using tinker to save object


This is my migration, I m using mysql, and when I run the migration, it success and I look at my mysql DB, the table's name is "receivedDocument" as I wanted.

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');
    }
}

And when I use tinker to create and save a record, it fails. Tinker doesn't seems to know I have change the table name in my migration.

>>> $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))'

How can I tell tinker what I have change in my migration?


Solution

  • In your model, define table as following:

    protected $table = 'receivedDocument';