baguus
126
7
Laravel

Update pivot table once again

Posted 1 year ago by baguus

Hi I know this question was asked and answered already but I cant find the working solution. I have 3 tables: users, roles, role_user

users model

public function roles()
    {
        return $this
            ->belongsToMany('App\Role')
            ->withTimestamps();
    }

roles model

    public function users()
    {
        return $this
            ->belongsToMany('App\User')
            ->withTimestamps();
    }

controller

public function update(Request $request, $id)
    {

$user = DB::table('users')
            ->where('id', $id)
            ->update([
                'first_name' => $request['first_name'],
                'last_name' => $request['last_name'],
                'email' => $request['email'],
                'companies_id' => $request['companies'],
            ]);

// How to update the record in role_user table?
  
return redirect('/');
}

Is there a simple way to update the record in role_user table? I was trying with detach(), attach(), sync(),...

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

Reply to

Use Markdown with GitHub-flavored code blocks.