[英]Laravel - Table does not exist
I create a table with php artisan make:migration
command.我用php artisan make:migration
命令创建了一个表。 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 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?问题是什么?
因为你不遵循Laravel 命名约定,你应该手动定义表:
protected $table = 'your_table_name';
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
@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';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.