I have the following db structure:
items:
id, name, user_id
users table:
id, name
user_favorites table:
id, user_id, item_id
On my items perm
The simplest way to do is to use toggle() method of many-to-many relationship.
e.g.
$user->roles()->toggle([1, 2, 3]);
The many-to-many relationship also provides a toggle method which "toggles" the attachment status of the given IDs. If the given ID is currently attached, it will be detached. Likewise, if it is currently detached, it will be attached
It also returns an array which tells you if the ID is attached or detached in DB.