belongsToMany(); is what you need (Many to Many thru pivot table, example in docs if you need)
Related Products Relationship Issue
I would like to something that I am sure is very simple - but I can't figure it out.
I have a 'products' table and a 'related_products' table.
products
-------------
id
name
desc
related_products
------------------------
product_id
related_product_id
The idea is simple - any given product can have any number of related products.
eg:
Product | Related Product
--------------------------------------
1 | 2
1 | 3
2 | 1
2 | 3
The trouble is I can't figure what the relations should be so that I can pull them.
At the moment:
Product Model
public function relatedProducts()
{
return $this->hasMany(RelatedProduct::class,'product_id');
}
I would like to be able to do this (but it only returns the related_product_id (but this makes sense as it is not a pivot):
@foreach ($product->relatedProducts as $product)
{{ $product->name }}
@endforeach
Ta
You were right on the relation type - I got there in the end via this reply by @TaylorOtwell in a Github issue.
https://github.com/laravel/framework/issues/441#issuecomment-14213883
Thanks Guys.
Please or to participate in this conversation.