laravel 未找到基表或视图:1146 表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51966767/
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 17:59:44  来源:igfitidea点击:

Base table or view not found: 1146 Table

laravel

提问by X Roney Cyberking

Error:

错误:

Illuminate \ Database \ QueryException (42S02) SQLSTATE[42S02]: Base table or view not found: 1146 Table 'mmictltd.admins' doesn't exist (SQL: select * from adminswhere email= [email protected] limit 1)

Illuminate \ Database \ QueryException (42S02) SQLSTATE[42S02]: Base table or view not found: 1146 Table 'mmictltd.admins' 不存在(SQL:select * from adminswhere email= [email protected] limit 1)

My create_admin_table.php

我的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

Laravel 错误说明

回答by Rafal Migda

Your table's in migration is called 'admin' but in query you are looking for 'admins'.

您正在迁移的表被称为“admin”,但在查询中您正在寻找“admins”。

You can specific table name in your model by $table:

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

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

Laravel convention is that table names should be plural in this case: https://laravel.com/docs/5.6/eloquent

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

So I recommend you to change your migration from 'admin' to 'admins'.

因此,我建议您将迁移从“admin”更改为“admins”。

回答by RAUSHAN KUMAR

By default laravel uses the "snake case", plural name of the class will be used as the table name unless another name is explicitly specified. So inside your Admineloquent model you must define the $tableproperty for your case as

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

protected $table = 'admin';

Check the eloquent model class convetion here https://laravel.com/docs/5.6/eloquent#eloquent-model-conventions

在此处检查 eloquent 模型类对流https://laravel.com/docs/5.6/eloquent#eloquent-model-conventions

回答by Evan Gertis

I ran into a similar difficulty trying to deploy my application on App engine. I will share with you how I fixed it.

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

  • remove routes from api.php (I didn't need these for my application)
  • enable cloud SQL api enable
  • Follow this tutorial tutorialfollow next two steps before deploying
  • make the following changes to the composer.json file. The tutorial is incorrect.
  • "post-install-cmd": [
                "Illuminate\Foundation\ComposerScripts::postInstall",
                "php artisan optimize",
                "chmod -R 755 bootstrap\/cache"
            ]
    
  • Configure your app.yaml file like so:
  • 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"
    
  • 从 api.php 中删除路由(我的应用程序不需要这些路由)
  • 启用云 SQL api启用
  • 遵循本教程教程,在部署之前遵循接下来的两个步骤
  • 对 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"