laravel Illuminate\Database\QueryException 带有消息“SQLSTATE[42S02]:未找到基表或视图:1146 表“test.people”不存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46190717/
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
Illuminate\Database\QueryException with message 'SQLSTATE[42S02]: Base table or view not found: 1146 Table 'test.people' doesn't exist
提问by Kamrul
i made a migration named persons while i want to import data useing tinker then error showing i made a migration named persons while i want to import data useing tinker then error showing Illuminate\Database\QueryException with message 'SQLSTATE[42S02]: Base table or view not found: 1146 Table 'test.people' doesn't exist (SQL: selec
当我想使用 tinker 导入数据时,我进行了名为 people 的迁移,然后错误显示我进行了名为 people 的迁移,而我想使用 tinker 导入数据,然后错误显示 Illuminate\Database\QueryException 并带有消息“SQLSTATE[42S02]: Base table or未找到视图:1146 表“test.people”不存在(SQL:selec
回答by Alexey Shabramov
This error can be caused when your database table has not created correctly. You must to create your models and migration files first.
当您的数据库表未正确创建时,可能会导致此错误。您必须首先创建模型和迁移文件。
To create them you can run this command (for example):
要创建它们,您可以运行此命令(例如):
php artisan make:model People -m
Then, after all configurations in the People model class and in the migration file - run:
然后,在 People 模型类和迁移文件中的所有配置之后 - 运行:
php artisan migrate
This will create all the migration tables in the database. p.s. don't forget to check your .env file for the correct database name.
这将在数据库中创建所有迁移表。ps 不要忘记检查您的 .env 文件以获取正确的数据库名称。
...
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=<your_database_name>
DB_USERNAME=<your_username>
DB_PASSWORD=<your_password>
...
回答by zaffar
One should be sure what model name are they using to create a model and what the error is coming. LARAVEL naming convention adds-up 's' assuming the table name ends with 's', e.g, in case you create a model with the name of 'dummy_project' as well as you have table 'dummy_project' it will add 's' to the end and if you run App\dummy_project in the tinker, it will produce the error:
人们应该确定他们使用什么模型名称来创建模型以及即将出现的错误。LARAVEL 命名约定添加 's' 假设表名以 's' 结尾,例如,如果您创建一个名为 'dummy_project' 的模型以及您有表 'dummy_project',它将添加 's'最后,如果您在 tinker 中运行 App\dummy_project,它将产生错误:
*Illuminate/Database/QueryException with message 'SQLSTATE[42S02]: Base table or view not found: 1146 Table 'tutorial.dummy_projects' doesn't exist (SQL: select * from dummy_projects
)'*
*Illuminate/Database/QueryException with message 'SQLSTATE[42S02]: Base table or view not found: 1146 Table 'tutorial.dummy_projects' 不存在 (SQL: select * from dummy_projects
)'*
All we need to do is to add the following line of code into the model: protected $table = 'table_name';=== explaination ===
我们需要做的就是将以下代码行添加到模型中: protected $table = 'table_name'; === 解释 ===
In case your table name is 'dummy_project'and you created a model name is 'dummy_project'too, the code in the model should be like following: *
如果您的表名是“dummy_project”并且您创建的模型名称也是“dummy_project”,则模型中的代码应如下所示:*
**<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class dummy_project extends Model
{
**protected $table = 'dummy_project';**
}**
* Please bear in mind, the model_name can be different than the table name, but 'protected $table = 'table_name'' should always refer to the relevant table name in the DATABASE!
* 请记住,model_name 可以与表名不同,但 'protected $table = 'table_name'' 应始终引用数据库中的相关表名!