Integrity constraint violation: How to join two tables

Viewed 899

I am making a program for a school project in laravel, so I try to join two tables: products and destinations

The table Product has the columns: id, name

The Destinations table has this columns: Destinations:id,product_id,destination,quantity,target_person

and I need to join product_id and id

products = DB::table('products')
    ->leftJoin('destinations','id','=','destinations.product_id ')
    ->get();

but when I try to use LEFT JOIN I get the following error:

SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in on clause is ambiguous (SQL: select * from products inner join destinations on id = destinations.product_id)

2 Answers

use Table reference products.id

products = DB::table('products')
    ->leftJoin('destinations','products.id','=','destinations.product_id')
    ->get();

Is because it does not know if 'id' is refering to the one in products or destinations. Try this:

products = DB::table('products')
->leftJoin('destinations','products.id','=','destinations.product_id')

->get();
Related