Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

thinkjay's avatar

Trouble refreshing my migrations

I deleted my database and recreated it, and tried to run the migrations: php artisan migrate:reset But I'm getting these errors below.

[Illuminate\Database\QueryException]
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'myapp.migrations' doesn't exist (SQL: select max(`batch`) as aggregate from `migrations`)

[PDOException]
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'myap.migrations' doesn't exist

I remember having a migrations table on the old database, how do I prevent this error so that the migrations can run?

0 likes
9 replies
mstnorris's avatar

You can't run reset when it is already empty.

php artisan migrate // runs the migrations only
php artisan migrate --seed // runs the migrations and seeds the database
php artisan migrate:rollback // undoes the last migration
php artisan migrate:reset // resets everything (if there is a migrations table)
php artisan migrate:refresh // removes everything except the migrations table
thinkjay's avatar

Thanks, @mstnorris I ran php artisan migrate and received this error:

[ErrorException] Undefined variable: table

I don't know what im supposed to do with such little error information, could you point me in the right direction? Thanks!

mstnorris's avatar

Paste you migration file here

It sounds like you are missing a $ before the word table in your migration file.

thinkjay's avatar

@mstnorris thanks for your help - here are my 4 migrations

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateQuestionsTable extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('questions', function(Blueprint $table)
        {
            $table->increments('id');
            $table->string('title');
            $table->string('metric');
            $table->string('method');
            $table->string('preferrable');
            $table->string('acceptable');
            $table->timestamps();
        });
    }

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

}

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateAnswersTable extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('answers', function(Blueprint $table)
        {
            $table->increments('id');
            $table->string('answer');
            $table->integer('user_id')->unsigned(); 
            
            $table->integer('question_id')->unsigned();
            $table->foreign('question_id')
              ->references('id')->on('questions')
              ->onDelete('cascade');
                });

            $table->timestamps();
        


    }

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

}

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function(Blueprint $table)
        {
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password', 60);
            $table->tinyInteger('roleType');
            $table->string('demo1');
            $table->string('demo2');
            $table->string('demo3');
            $table->string('demo4');
            $table->string('demo5');
            $table->rememberToken();
            $table->timestamps();

        });
    }

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

}

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreatePasswordResetsTable extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('password_resets', function(Blueprint $table)
        {
            $table->string('email')->index();
            $table->string('token')->index();
            $table->timestamp('created_at');
        });
    }

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

}

mstnorris's avatar

I don't see anything wrong there

Can you manually empty your database, and run the migrations again. Then tell me what you get.

thinkjay's avatar

@mstnorris

php artisan migrate

Migration table created successfully.

  [ErrorException]
  Undefined variable: table

mstnorris's avatar
Level 55

@thinkjay I've spotted it:

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateAnswersTable extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('answers', function(Blueprint $table)
        {
            $table->increments('id');
            $table->string('answer');
            $table->integer('user_id')->unsigned(); 
            
            $table->integer('question_id')->unsigned();
            $table->foreign('question_id')
              ->references('id')->on('questions')
              ->onDelete('cascade');
                }); // HERE, move it below $table->timestamps();

            $table->timestamps();
        


    }

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

}
thinkjay's avatar

@mstnorris oh my - so much time wasted because of that! thanks so much it was driving me crazy.

Please or to participate in this conversation.