duanli9569 2015-10-11 09:06 采纳率: 100%
浏览 609

多对多关系,laravel sync方法

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)))
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测