Laravel - 表不存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42833271/
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
Laravel - Table does not exist
提问by user3242861
I create a table with php artisan make:migration
command. After that i edit the migration file to put the columns and insert standard data.
After i run php migrate:referesh
command and all good, i go to the database and the table is there with a correct name and data.
我用php artisan make:migration
命令创建了一个表。之后我编辑迁移文件以放置列并插入标准数据。在我运行php migrate:referesh
命令并且一切正常之后,我转到数据库并且该表在那里具有正确的名称和数据。
In database the table name is rolesStems.
在数据库中,表名是rolesStems。
I create a model like this:
我创建了一个这样的模型:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class RolesStems extends Model
{
protected $fillable = ['id', 'value', 'order', 'active','id_stem', 'id_role'];
}
And in my controller i do this:
在我的控制器中,我这样做:
use App\RolesStems as RolesStem;
RolesStem::select('value')->where('active', '=', 1)->where('id_stem', '=', 1)->orderBy('id_role', 'asc')->get());
And i have this error:
我有这个错误:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'dbname.roles_stems' doesn't exist (SQL: select `value` from `roles_stems` where `active` = 1 and `id_stem` = 1 order by `id_role` asc)
It put a unknown table name roles_stems, why?
它放了一个未知的表名roles_stems,为什么?
I do this to other table, the same way, but with this table this happens.
我以相同的方式对其他表执行此操作,但是在该表中会发生这种情况。
What is the problem?
问题是什么?
回答by Alexey Mezenin
Because you don't follow Laravel naming conventions, you should define table manually:
因为你不遵循Laravel 命名约定,你应该手动定义表:
protected $table = 'your_table_name';
回答by Mayank Pandeyz
For table with the name rolesStems
your model
must have the name RolesStem
对于带有名称rolesStems
的表,您model
必须具有名称RolesStem
So change the following line:
所以更改以下行:
class RolesStems extends Model
to
到
class RolesStem extends Model
This is the default Laravel naming convention: Reference
这是默认的 Laravel 命名约定:参考
If you don't want to follow this naming convention, then put the following code in your model:
如果您不想遵循此命名约定,则将以下代码放入您的模型中:
protected $table = 'your_table_name';
So that your model refers to your_table_name
所以你的模型是指 your_table_name
回答by PHP Team
@user3242861, By default Laravel Try to find table according to Your Model Class name eg: your class "RolesStems" then it will try to find table "roles_stems" so if it is not available then it will give table or view not exists.
@ user3242861,默认情况下,Laravel 尝试根据您的模型类名称查找表,例如:您的类“RolesStems”然后它会尝试查找表“roles_stems”,因此如果它不可用,那么它将给出表或视图不存在。
To override default functionality you can define table name explicitly in your model class as below.
要覆盖默认功能,您可以在模型类中显式定义表名称,如下所示。
protected $table = 'your_table_name';