gpt4 book ai didi

php - Laravel SQLSTATE[42000] : Syntax error or access violation

转载 作者:行者123 更新时间:2023-11-29 13:42:22 33 4
gpt4 key购买 nike

我是 Laravel 的新手,我一直与我的关系人保持联系,如下所示

类别

    id  name            slug
-----------------------------------------------------------------
3 Location location
4 Outfits outfits
5 Other other

子类别

  id    category_id     name                slug
-----------------------------------------------------------------------------
12 3 Club club
13 3 Home / Hotel home-hotel
14 3 Outdoor outdoor
15 3 Studio studio
16 4 Bikini / Swimwear bikini-swimwear
17 4 Dress dress
19 4 Jeans jeans
35 5 Dancing dancing

类别模型

<?php

class Category extends Eloquent {

public $timestamps = false;

public function subcategory()
{
return $this->belongsToMany('subcategory', "sub_categories");
}
}

我收到以下错误

SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'sub_categories' (SQL: select `sub_categories`.*, `sub_categories`.`category_id` as `pivot_category_id`, `sub_categories`.`subcategory_id` as `pivot_subcategory_id` from `sub_categories` inner join `sub_categories` on `sub_categories`.`id` = `sub_categories`.`subcategory_id` where `sub_categories`.`category_id` = ?) (Bindings: array ( 0 => 1, )) 

有人可以指出我做错了什么吗?

最佳答案

您应该为同一个表指定不同的别名

改变这个

 inner join `sub_categories`

   inner join `sub_categories` as sc   
^^--//-this alias use it instead of sub_categories

在您的查询中它将是

    select `sub_categories`.*, sc.`category_id` as `pivot_category_id`, sc.`subcategory_id` as `pivot_subcategory_id` from `sub_categories` inner join `sub_categories` sc on sc.`id` = `sub_categories`.`subcategory_id` where `sub_categories`.`category_id` = ?) (Bindings: array ( 0 => 1, )

示例:

 select * from table1 as t1
inner join table1 as t2
on t1.id = t2.id

错误说明: 表/别名不唯一

您正在将同一个表连接到不同的表,而无需使用别名。

关于php - Laravel SQLSTATE[42000] : Syntax error or access violation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17908445/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com