I'm having difficulty properly joining tables in L9. This joins my products and additionalProducts tables. The mission is to get all the products that can be added with productId(x). I have the Product table with all the info and additonalProduct that look like this:
| id | product_id | additionalProductId |
|---|---|---|
| 1 | 1 | 2 |
| 2 | 1 | 4 |
| 3 | 2 | 3 |
| 4 | 2 | 5 |
The meaning for product_id=1 I want to get all the info of the products with id 2 and 4 from 'product' table and for product_id=2 get the info of products 3 and 5.
This query is working when productId is hard coded('1' or '2') but when I tray make it dynamic for some reason I got error $productId undefined.
$productId = $productsInfo[0]->product_id;
(when i print it with dd it show id=1 or 2).
but inside the query it is undefined and I cannot understand why and how to fix it.
$viewData['additionalProducts'] = DB::table('products as p')
->join('addtionalproducts as ap', function ($join) {
$join->on('p.id', '=', 'ap.additionalProductId')
->select('p*')
->where('ap.product_id', '=', $productId);
})->get();