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
Base table or view not found: 1146 Table
提问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
admins
where
Illuminate \ Database \ QueryException (42S02) SQLSTATE[42S02]: Base table or view not found: 1146 Table 'mmictltd.admins' 不存在(SQL:select * from
admins
where
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');
}
}
回答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"
]
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"
"post-install-cmd": [
"Illuminate\Foundation\ComposerScripts::postInstall",
"php artisan optimize",
"chmod -R 755 bootstrap\/cache"
]
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"