Is there simpler way to update pivot table fields

Posted 2 years ago by swalker

Hello,

so, this is the context: user can be in many teams teams can have many members(users) une of the members is the team's admin

and then I want to change the admin

I got it working like this but its kind of nasty: in my Team model

    public function setAdmin(User $user)
    {
        $this->members()->wherePivot('is_admin', 1)->first()->pivot->update(['is_admin' => false]);
        $this->members()->wherePivot('user_id', $user->id)->first()->pivot->update(['is_admin' => true]);
    }

This feels just wrong I couldn't find a better way I swear I searched a lot

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.