Laravel - Relationships - Toggling Associations

The many-to-many relationship also provides a toggle method which "toggles" the attachment status of the given related model IDs. If the given ID is currently attached, it will be detached. Likewise, if it is currently detached, it will be attached:

    
    $user->roles()->toggle([1, 2, 3]);