Sunday, March 12, 2017

How do I connect a "related products" table

I have the following tables

products table

product_id
name
desc

with a has many relation to

suggested products table

id
product_id
suggested_product_id 

What I am trying to accomplish is being able to access the name of a suggested product id in my view.

Products Controller

public function show($id)
{
$product = Product::with('stockitems','suggestedproducts.product')->find($id);

  return view('backend.products.show', compact('product'));
}

I know the issue is at "suggestedproducts.product" just not sure how i specify the key for the eager load is a product_id.



via DevJustin

Advertisement