laravel 4 updateExistingPivot with where子句无效


laravel 4 updateExistingPivot with where clause not working

我有两种型号:

用户资源

关系表是resource_user

resource_user中的字段为:

id | resource_id | user_id | another_id

我在用户中有这样的关系:

public function resources() {
        return $this->belongsToMany('Resource')->withPivot(array(
            'value',
            'another_id',
        ));
    }

现在我想更新我的数据透视表:

(在模型用户中有此代码示例)

$this->resources()->whereAnotherId(1)->updateExistingPivot($resource_id, array(
                            'value' => $value,
                            'updated_at' => new DateTime,
                        ));

问题出在another_id上。

如果我的关系表(resource_user)中有两个条目但与不同另一个id的s。在本例中,laravel将更新BOTH条目。但这不是我想要的。在本例中,只应更新一个条目(another_id=1的条目)。这是一个错误吗,或者我如何更新我的透视表(sync()函数在这里的表设置中不起作用)。。

请尝试使用wherePivot()。使用whereAnotherId(1),您的目标是Resource的表,而不是透视表。。。

$this->resources()->wherePivot('another_id', 1)
                  ->updateExistingPivot($resource_id, array(
                        'value' => $value,
                        'updated_at' => new DateTime,
                    ));