我正在尝试在表之间创建一个简单的关系:
- attribute_type -
id
name
- category -
id
name
description
Run Code Online (Sandbox Code Playgroud)
所以我创建了一个数据透视表来链接它们:
- attribute_type_category -
attribute_type_id
category_id
Run Code Online (Sandbox Code Playgroud)
有模型关系:
在 AttributeType.php 上
public function category() {
return $this->belongsToMany('App\AttributeTypeCategory', 'attribute_type_category', 'attribute_type_id', 'category_id');
}
Run Code Online (Sandbox Code Playgroud)
在 AttributeTypeCategory.php 上
public function category() {
return $this->belongsToMany('App\Category');
}
Run Code Online (Sandbox Code Playgroud)
一切似乎都很好,但我收到以下错误:
SQLSTATE[42000]:语法错误或访问冲突:1066 不是唯一的表/别名:'attribute_type_category'(SQL:select
attribute_type_category.*,attribute_type_category.attribute_type_idaspivot_attribute_type_id,attribute_type_category.category_idaspivot_category_idfromattribute_type_categoryinternal joinattribute_type_categoryonattribute_type_category.id=attribute_type_category.category_idwhereattribute_type_category.attribute_type_id= 1)
你有什么主意吗 ?谢谢 !