laravel 4 updateExistingPivot with where 子句不起作用

laravel 4 updateExistingPivot with where clause not working

我有两个模型:

用户资源

关系table是resource_user.

resource_user中的字段是:

id | resource_id | user_id | another_id

我在用户中有这个关系:

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

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

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

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

问题是another_id。

如果我的关系中有两个条目 table (resource_user) 但是 不同 another_id 的。在此示例中,laravel 将更新两个条目。但这不是我想要的。 在此示例中,仅应更新一个条目(another_id = 1 的条目)。 这是一个错误,还是我如何更新我的数据透视表 table(sync() 函数不适用于我在此处的 table 设置)..

请尝试使用 wherePivot()。使用 whereAnotherId(1),您的目标是 Resource 的 table 而不是枢轴 table...

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