【问题标题】:Base table or view not found: 1146 Table未找到基表或视图:1146 表
【发布时间】:2019-01-28 17:02:00
【问题描述】:

错误:

照亮\数据库\查询异常(42S02) SQLSTATE [42S02]:未找到基表或视图:1146 表 'mmictltd.admins' 不存在(SQL:select * from admins where email = kayondoronald2015@gmail.com 限制 1)

我的create_admin_table.php

<?php

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

class CreateAdminTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('admin', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

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

Laravel error illustration

【问题讨论】:

  • 我想知道为什么它说 select * from admins 但我的表是 admin

标签: laravel


【解决方案1】:

我在尝试在 App 引擎上部署我的应用程序时遇到了类似的困难。我将与您分享我是如何修复它的。

  • 从 api.php 中删除路由(我的应用程序不需要这些)
  • 启用云SQL APIenable
  • 按照本教程tutorial 在部署前按照接下来的两个步骤进行
  • 对 composer.json 文件进行以下更改。教程不正确。
"post-install-cmd": [
            "Illuminate\\Foundation\\ComposerScripts::postInstall",
            "php artisan optimize",
            "chmod -R 755 bootstrap\/cache"
        ]
  • 像这样配置您的 app.yaml 文件:
  • runtime: php
    env: flex
    
    runtime_config:
      document_root: public
    
    env_variables:
      # Put production environment variables here.
      APP_ENV: production
      APP_LOG: errorlog
      APP_KEY: APP_KEY (DO NOT USE QUOTES)
      CACHE_DRIVER: database
      SESSION_DRIVER: database
      ## Set these environment variables according to your CloudSQL configuration.
      DB_HOST: localhost
      DB_PORT: 3306
      DB_CONNECTION: mysql
      DB_DATABASE: DATABASE_NAME (DO NOT USE QUOTES)
      DB_USERNAME: USERNAME (DO NOT USE QUOTES)
      DB_PASSWORD: PASSWORD (DO NOT USE QUOTES)
      DB_SOCKET: /cloudsql/YOUR_INSTANCE_CONNECTION_NAME (DO NOT USE QUOTES)
    
      QUEUE_DRIVER: database
    
    beta_settings:
        # for Cloud SQL, set this value to the Cloud SQL connection name,
        # e.g. "project:region:cloudsql-instance"
        cloud_sql_instances: "YOUR_INSTANCE_CONNECTION_NAME"
    

    【讨论】:

      【解决方案2】:

      默认情况下,laravel 使用 "snake case",除非明确指定另一个名称,否则类的复数名称将用作表名。因此,在您的 Admin 雄辩模型中,您必须为您的案例定义 $table 属性为

      protected $table = 'admin';
      

      在这里查看雄辩的模型类对流https://laravel.com/docs/5.6/eloquent#eloquent-model-conventions

      【讨论】:

        【解决方案3】:

        您的表在迁移中称为“管理员”,但在查询中您正在寻找“管理员”。

        您可以通过 $table 在模型中指定表名:

        /**
         * The table associated with the model.
         *
         * @var string
         */
        protected $table = 'admin';
        

        Laravel 约定在这种情况下表名应该是复数:https://laravel.com/docs/5.6/eloquent

        因此,我建议您将迁移从“管理员”更改为“管理员”。

        【讨论】:

        • @OP 虽然,我还是建议您遵守 Laravel 的约定并阅读文档以理解它们。
        猜你喜欢
        • 2017-08-02
        • 2014-02-25
        • 1970-01-01
        • 2023-03-26
        • 2015-07-21
        • 2021-05-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多