如何从laravel 5.6中的数据透视表更新特定行?

时间:2018-08-28 06:10:18

标签: php laravel laravel-5

我在Organization.php中有此模型

public function agency()
{
    return $this->belongsToMany('App\Agency')->withPivot('status');
}

以及Agency.php中的此模型

public function organizations()
{
    return $this->belongsToMany('App\Organization')->withPivot('status');
}

如何更新数据透视表中特定行的状态? 这是我在控制器中的代码:

public function removeAgency(Request $request){
    $my_agencies = Organization::find($request['org_id']);
    $member = $my_agencies->agency()
        ->where('agency_organization.agency_id', $request['agency_id'])
            ->first();
    $member->pivot->status = 'removed';
    $member->pivot->save();
}

它返回一条错误消息:“从空值创建默认对象”

1 个答案:

答案 0 :(得分:2)

您可以使用updateExistingPivot方法。

domain2.example
相关问题