当前位置: 首页 > 知识库问答 >
问题:

未找到Laravel基表或视图

习淇
2023-03-14

Im my routes.php我有以下内容:

<?php

Route::group(array(), function () {
    View::share('roots', Category::roots()->get());
    $tree = Category::get()->toHierarchy()->toArray();
    View::share('categories', $tree);

    Route::get('/', array('as' => 'home', 'uses' => 'HomeController@index'));
});

当我的数据库还没有表,我想进行php artisan迁移时,结果是:SQLSTATE[42S02]:找不到基表或视图:1146表“ae_dev.categories”不存在

我的迁移文件:

<?php

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

class CreateCategoriesTable extends Migration {

  /**
   * Run the migrations.
   *
   * @return void
   */
  public function up() {
    Schema::create('categories', function(Blueprint $table) {
      $table->increments('id');
      $table->integer('parent_id')->nullable()->index();
      $table->integer('lft')->nullable()->index();
      $table->integer('rgt')->nullable()->index();
      $table->integer('depth')->nullable();
      $table->string('name', 255);

      $table->timestamps();
    });
  }

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

}

我认为Laravel托盘从routes.php调用类别,想做选择或某种想法,所以我想运行创建类别表的迁移,但上述错误是在...

我怎样才能解决这个问题?

共有1个答案

秦鸿羽
2023-03-14

似乎所有的php artisan命令都使用routes.php文件,因此当您尝试在该文件中访问数据库表时(由于没有运行迁移,所以表不存在),您将遇到此错误。您无法运行php artisan migrate,因为您会遇到此错误。

一种解决方案是删除查询数据库的代码,但这当然不是好的解决方案。因此,您应该做的是从第一次迁移中选择表(在您的情况下,可能是类别。稍后你有更多的迁移,但这将是第一次),并添加类似的内容:

if (!Schema::hasTable('categories'))
{
    return;
}

进入你的routes.php文件。

但是,如果要更多地使用迁移,并且还需要其他表来查询,则需要将上述条件更改为:

if (!Schema::hasTable('categories') || !Schema::hasTable('users'))
{
    return;
}

但它仍然会导致一些问题-您不希望每次在路由中都运行此代码,因此我会这样做:

if ($env == 'local') {

    if (!Schema::hasTable('categories') || !Schema::hasTable('users'))
    {
        return;
    }
}

当然,您需要配置您的环境。但现在您只在本地环境中运行此代码,在生产环境中不会运行此代码,因此不会影响应用程序性能。当然,我在这里假设你不会在生产中与artisan合作。

编辑

但是,如果您仅将查询用于共享数据(而不用于路由),我将移动这些行:

View::share('roots', Category::roots()->get());
$tree = Category::get()->toHierarchy()->toArray();
View::share('categories', $tree);

BaseController,并在扩展它的所有控制器中运行方法(或父构造函数)。

旧答案(在这种情况下不充分)

错误信息在您的解释中已经足够清楚,您的数据库中还没有表。如果没有表,则无法运行html" target="_blank">数据库查询。

在迁移过程中,您应该创建必要的表,例如,这是针对用户表的迁移:

<?php

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

class NewUser extends Migration
{

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function ($table) {
            $table->engine = 'InnoDB';
            $table->increments('id');
            $table->string('user_name', 60)->unique();
            $table->string('email', 120)->unique();
            $table->string('passwd', 256);
            $table->decimal('balance', 8, 2);
            $table->rememberToken();
            $table->timestamps();
        });

    }

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

}

在使用迁移时,也不应该向数据库中插入数据(现在可能会这样做)。您应该在为表设定种子时执行此操作。

 类似资料:
  • 我在我的电脑上安装了homestead,然后我尝试通过本地域访问我的文件。但是当我尝试连接DB时,会显示这个错误。我也无法访问我的其他项目。我把一切都安排得很好。但我不知道为什么给我看这个错误。 注意:我使用的是Windows 8.1 这是我的设置代码: 亚马尔家园酒店 ip:“192.168.10.10” 内存: 2048 CPI: 1 提供者:virtualbox 授权:~/.ssh/id\u

  • 当我尝试使用Laravel 5将数据保存到mysql时,我得到了这个错误,其他形式和保存()方法工作正常,但这一个: 这是我的Controller存储方法: 这是我的模型: 我一定是忽略了一些非常明显的事情,因为我不明白为什么拉雷维尔会加上一个“S”这个词。这个表是cotizacion而不是cotizacions。 如何对此进行故障排除?

  • 问题内容: 我在尝试使用Laravel 5将数据保存到mysql时收到此错误消息,其他形式和save()方法都可以正常工作,但是以下一种方法: 这是我的Controller存储方法: 这是我的模型: 我必须忽略一些确实很明显的原因,因为我无法理解Laravel为什么在表中添加cot而不是cotizacion。 我该如何解决? 问题答案: 我猜想Laravel无法确定您用于表名的单词的复数形式。 只

  • 首先,我错误地回滚了2次迁移,然后运行了命令,出现以下错误: 然后我阻止了拉威尔。之后,当我运行启动Laravel的命令时,我得到了相同的错误。以下是我回滚的两个迁移: 1. 2. 请帮我解决这个令人沮丧的问题。所有答案都非常感谢,提前感谢。

  • 我刚刚克隆了一个Laravel项目。我试图运行作曲家安装和PHP artisan迁移,但都返回此错误 在Connection.php行664中: SQLSTATE[42S02]:基表或视图未找到: 1146表'name.system_functions'不存在(SQL:选择*from其中=测试限制1) 在Connection.php第326行中: SQLSTATE[42S02]:找不到基表或视图:

  • 我创造了一个迁移 任何我写的CMD这个错误是可见的。 [Illumb\Database\QueryException]SQLSTATE[42S02]:找不到基表或视图:1146表“prj_roocket.permissions”不存在(SQL:select*from) [PDOException]SQLSTATE[42S02]:未找到基表或视图:1146表“prj_roocket.permissi