Home > Software engineering >  Inner join error - Column 'id' in where clause is ambiguous
Inner join error - Column 'id' in where clause is ambiguous

Time:07-28

I had made a product filter, but I had to update.

Before, the url was passed a slug and an id. I used this id to get the subcategories and in this table of subcategories a column called 'line_id' is saved. I took the id passed in the url and looked for what was equal to this line_id. This line_id is for the products page, which has some categories and the subcategories corresponding to each of them.

Now I have to do it differently, I have to take only one subcategory and select the products corresponding to that subcategory.

In the products table, the id of each subcategory is already saved. So I tested it manually, passing an id of a subcategory, but it gives an error when doing the inner join.

public function nossas_marcas($slug, $id)
    {
        $menu_path = 'nossas_marcas';
        $category = Categorie::find($id);
        $lines = Line::where('situation', true)->get();

        $ProductsCategories = ProductsCategory::where('id', 3);
        $brandProducts = $ProductsCategories
                            ->join('products', 'products_categories.id', '=', 'products.product_category_id')->get();
        // $brandProducts = $ProductsCategories
        //                     ->join('products', 'products_categories.id', '=', 'products.product_category_id')
        //                     ->take(4)->orderBy(\DB::Raw('rand()'))->get(['products_categories.id', 'products_categories.title as product_category_name', 'products.title as product_title', 'products.id', 'products.ecommerce_link']);

        return view('site.Nossas-marcas.index', compact(['menu_path', 'category', 'lines', 'brandProducts']));
    }

Error

Illuminate\Database\QueryException
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous (SQL: select * from `products_categories` inner join `products` on `products_categories`.`id` = `products`.`product_category_id` where `id` = 3)

I can't understand the reason for the error, someone explain to me please.

CodePudding user response:

You should tell him what table do you mean. The correct form is products_categories.id, not id.

ProductsCategory::where('id', 3);
  • Related