无法启动 Laravel,出现“未找到基表或视图”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42511841/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:26:30  来源:igfitidea点击:

Can't start Laravel, I get "Base table or view not found" error

phplaravellaravel-migrations

提问by Mahammad Isgandarli

First I rolled back 2 migrations by mistake, then I ran php artisan migratecommand and I got the following error:

首先,我错误地回滚了 2 个迁移,然后我运行了php artisan migrate命令并收到以下错误:

[Illuminate\Database\QueryException] SQLSTATE[42S02]: Base table or view not found: 1146 Table 'exercise1.categories' doesn't exist (SQL: select * from categorieswhere parent_id= 0) [PDOException] SQLSTATE[42S02]: Base table or view not found: 1146 Table 'exercise1.categories' doesn't exist

[Illuminate\Database\QueryException] SQLSTATE[42S02]: Base table or view not found: 1146 Table 'exercise1.categories' doesn't exist (SQL: select * from categorieswhere parent_id= 0) [PDOException] SQLSTATE[42S02]: Base table or view not found: 1146 Table 'exercise1.categories' doesn't exist

Then I stopped Laravel. After that when I run the php artisan servecommand for starting Laravel I get the same error. Here are 2 migrations which I've rolled back:

然后我停止了 Laravel。之后,当我运行php artisan serve启动 Laravel的命令时,我得到了同样的错误。这是我回滚的 2 个迁移:

1.

1.

class CreateCategoriesTable extends Migration
    {

        public function up()
        {
            Schema::create('categories',function (Blueprint $table){
                $table->increments('id');
                $table->string('name');
                $table->text('parent_id');
                $table->timestamps();
        });
        }
        public function down()
        {
            Schema::dropIfExists('categories');
        }
    }

2.

2.

class CreateArticlesTable extends Migration
    {
        public function up()
        {
            Schema::create('articles', function (Blueprint $table) {
                $table->increments('id');
                $table->string('title')->nullable(false);
                $table->longText('article')->nullable(false);
                $table->integer('user_id')->unsigned();
                $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
                $table->timestamps();
            });
        }
        public function down()
        {
            Schema::dropIfExists('articles');
        }
    }

Please help me to solve this frustrating problem. All answers are highly appreciated, thanks in advance.

请帮我解决这个令人沮丧的问题。非常感谢所有答案,提前致谢。

回答by Mahammad Isgandarli

If you encounter with this problem and if it's not caused by migration files then most probably it happens because of 2 possible reasons.

如果您遇到此问题并且它不是由迁移文件引起的,那么很可能是由于 2 个可能的原因造成的。

  1. Check ServiceProviders' boot function if it contains queries that are querying tables that don't exist.
  2. Check if you've created custom helper functionand autoloaded that helper function in composer.json file. If custom helper function contains queries that are querying tables that don't exist it will cause this error.
  1. 检查ServiceProviders的启动函数,如果它包含查询不存在的表的查询。
  2. 检查您是否已创建自定义辅助函数并在 composer.json 文件中自动加载该辅助函数。如果自定义辅助函数包含查询不存在的表的查询,则会导致此错误。

Since ServiceProviders' boot functions and autoloaded custom helper functions are loaded first when laravel is started all the php artisancommands will generate "Base table or view not found" error.

由于 ServiceProviders 的引导函数和自动加载的自定义帮助函数在 Laravel 启动时首先加载,因此所有php artisan命令都会生成“找不到基表或视图”错误。

At this point what you should do is comment out those queries that are querying nonexistent tables and run php artisan servethen run php artisan migrate. Then uncomment those lines, save it and everything should work fine.

此时您应该做的是注释掉那些查询不存在的表并运行php artisan serve然后运行的查询php artisan migrate。然后取消注释这些行,保存它,一切都应该正常工作。

As @devk suggested it's better to check laravel log files which points exactly to where the problem happens. It led me to find a solution. For this don't forget to Turn on debug mode.

正如@devk 建议的那样,最好检查 laravel 日志文件,这些文件准确地指出了问题发生的位置。它让我找到了解决方案。为此不要忘记打开调试模式。

回答by Alexey Mezenin

When you've rolled back these migrations, you've deleted categoriesand articlestables. So just run migratecommand again:

当你回滚这些迁移,您已经删除categoriesarticles表。所以只需migrate再次运行命令:

php artisan migrate

回答by spencerstewart

For me, this error was occurring because of a scheduled taskin laravel that involved a database lookup. By commenting out that lookup in laravel/app/Console/Kernel.phpfrom my scheduled task, I was able to migrate my database again.

对我来说,发生此错误是因为laravel中的计划任务涉及数据库查找。通过laravel/app/Console/Kernel.php从我的计划任务中注释掉该查找,我能够再次迁移我的数据库。

回答by LaraCoder

This may happen due to any of the following problems:

这可能是由于以下任何问题造成的:

  • Your database setting are not correct. Please check your database settings again.
  • The database name is not correct. May be the database your code is trying to get tables from is not same as the database you mentioned in your database configuration.
  • 您的数据库设置不正确。请再次检查您的数据库设置。
  • 数据库名称不正确。可能是您的代码试图从中获取表的数据库与您在数据库配置中提到的数据库不同。