小编Had*_*ien的帖子

Laravel - 1066 不是唯一的关系表/别名

我正在尝试在表之间创建一个简单的关系:

- 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_idas pivot_attribute_type_id, attribute_type_category. category_idas pivot_category_idfrom attribute_type_categoryinternal join attribute_type_categoryon attribute_type_category. id= attribute_type_category. category_idwhere attribute_type_category. attribute_type_id= 1)

你有什么主意吗 ?谢谢 !

php sql relationship laravel eloquent

2
推荐指数
1
解决办法
1万
查看次数

标签 统计

eloquent ×1

laravel ×1

php ×1

relationship ×1

sql ×1