laravel eloquent 不使用受保护的表名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38143471/
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 eloquent doesn't use protected table name
提问by train_fox
In my model i added protected $table, but when i'm going to use it laravel does't use it. This is my role models:
在我的模型中,我添加了受保护的 $table,但是当我要使用它时,laravel 不使用它。这是我的榜样:
class Role extends Model
{
protected $table = 'role';
protected $primaryKey = 'ROLE_ID';
protected $casts = [
'ACTIVE' => 'boolean',
];
protected $fillable = [
'ROLE', 'ACTIVE', 'TYPE'
];
public $timestamps = false;
public function groups()
{
return $this->belongsToMany(Group::class, GroupRole::class, 'ROLE_ID', 'GROUP_ID');
}
}
And this is Group model:
这是组模型:
class Group extends Model
{
protected $table = 'groups';
protected $primaryKey = 'GROUP_ID';
protected $fillable = [
'GROUP_ID', 'GROUP_NAME', 'PARENT_GROUP', 'ACTIVE'
];
protected $casts = [
'ACTIVE' => 'boolean',
];
public $timestamps = false;
public function type()
{
return $this->belongsTo(GroupType::class, 'TYPE', 'TYPE_ID');
}
public function roles()
{
return $this->belongsToMany(Role::class, GroupRole::class, 'GROUP_ID', 'ROLE_ID');
}
}
And this is group_role table model. It handles many to many relation between role and group:
这是 group_role 表模型。它处理角色和组之间的多对多关系:
class GroupRole extends Model
{
protected $table = 'group_role';
protected $primaryKey = 'GROUP_ROLE_ID';
protected $fillable = [
'COMMENT', 'ROLE_ID', 'GROUP_ID'
];
public $timestamps = false;
}
Problem begin when i want to use this models. For example:
当我想使用这个模型时,问题就开始了。例如:
$role = App\Role::first();
$groups = $role->groups;
Laravel returns this error messages:
Laravel 返回此错误消息:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'favian_mydb.App\GroupRole' doesn't exist (SQL: select
groups
.*,App\GroupRole
.ROLE_ID
aspivot_ROLE_ID
,App\GroupRole
.GROUP_ID
aspivot_GROUP_ID
fromgroups
inner joinApp\GroupRole
ongroups
.GROUP_ID
=App\GroupRole
.GROUP_ID
whereApp\GroupRole
.ROLE_ID
= 1)
SQLSTATE[42S02]:未找到基表或视图:1146 表 'favian_mydb.App\GroupRole' 不存在(SQL:select
groups
.*,App\GroupRole
.ROLE_ID
aspivot_ROLE_ID
,App\GroupRole
.GROUP_ID
aspivot_GROUP_ID
from internalgroups
joinApp\GroupRole
ongroups
.GROUP_ID
=App\GroupRole
.GROUP_ID
whereApp\GroupRole
.ROLE_ID
= 1)
I tried to replace App\GroupRole with group_role and executing in mysql. It works fine. Am i missing something?
我试图用 group_role 替换 App\GroupRole 并在 mysql 中执行。它工作正常。我错过了什么吗?
回答by nahri
The Problem is in your roles
relation:
问题出在你的roles
关系上:
public function roles()
{
return $this->belongsToMany(Role::class, GroupRole::class,'GROUP_ID','ROLE_ID');
}
The belongsToMany
expects the intermediate tablename as second argument, not the classname.
的belongsToMany
期望中间表的名称作为第二个参数,而不是类名。
So you have to define it like this:
所以你必须像这样定义它:
public function roles()
{
return $this->belongsToMany(Role::class, 'group_role','GROUP_ID','ROLE_ID');
}
回答by Thomas Van der Veen
I think the problem is in you relation functions. Try to use strings instead of Model::class.
我认为问题在于你的关系函数。尝试使用字符串而不是 Model::class。
Example:
例子:
return $this->return $this->belongsTo('App\GroupType', 'TYPE', 'TYPE_ID');
return $this->return $this->belongsTo('App\GroupType', 'TYPE', 'TYPE_ID');
Hope this works.
希望这有效。