In my shop application I have 3 tables: products, categories and category_product
products:
id | product_id | lang | name
1 | 1 | pl | name
2 | 1 | en | name
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');
}
And now I enter: http://sklep.app/admin/products/2/edit - which is the product of "id" of 2.
When I hit update I want to add records to category_product:
category_id | product_id
1 | 1
2 | 1
3 | 1
because product_id = 1
instead I receive 3 records
1 | 2
2 | 2
3 | 2
How can I make my app set product_id to product_id value not the id value.
This is how I add categories to products:
$product->categories()->sync($request->categories, (sizeof($request->categories) >= sizeof($product->categories)))