I have a problem with getting everything working... I have 3 tables: products, categories, category_product - working as many to many relation.
inside product I have:
id | product_id | lang | name
1 | 1 | pl | name1
2 | 1 | en | name2
3 | 2 | pl | name3
4 | 2 | en | name4
categories:
id | category_id | lang | name
1 | 1 | pl | name
2 | 1 | en | name
...
This is my relation in Product model:
public function categories()
{
return $this->belongsToMany('App\Category', 'category_product', 'product_id', 'category_id');
}
I managed to make categories assignment to the product by:
$arr = [];
foreach($request->categories as $cat){
$arr[$cat] = array('product_id' => $product->product_id);
}
$product->categories()->sync($arr);
But now I have no idea how can I get categories in my product. I try as
$product->categories
but it still doesn't know how to connect product_id from products table with product_id from category_product.
http://sklep.app/admin/products/2/edit - here I edit my product with "id" of 2 and when I assign categories, I assign them to product_id in order to have them assigned to all lang versions in the same way.
I would appreciate your help :)