I have a screen where admin adds topics, db name is chat_topics
(id, title, status) then in edit topic screen, there are two lists with available users and assigned users.
When admins assigns users it saves in a pivot table, chat_topic_user
(chat_topic_id, users_id)
Now, one user can be assign to many topics. I am able to assign users to topics, then in edit screen I am able to display all the assigned users for that specific topic.
Problem comes when I assign a specific user it even shows in available users which should not display, for example if userA is assigned to TopicA then for TopicA edit screen it shouldn't display userA in available users, right.
But userA should be displayed on TopicB edit edit screen.
I hope I am clear enough to explain the problem.
Code to display all available users:
User::with('chat_topics')->get()
(if userA is assigned to TopicA, so userA shouldn't be displayed in available users, but it should be displayed for TopicB available list)
Code to display only assigned users on specific topic
User::has('chat_topics')->whereHas('chat_topics', function ($query) use ($topic_id) { $query->where('id', $topic_id); })->get();
I want something like this:
TOPIC A EDIT SCREEN
- Assigned Users
user a
user b
Available Users
empty (if only two users in db)TOPIC B EDIT SCREEN
- Assigned Users
empty
Available Users
user a
user b
Currently it displays like this:
TOPIC A EDIT SCREEN
- Assigned Users
user a
user b
- Available Users
user a
user b
Its duplicating, as one cant be assigned more than once for same topic.
Thanks in advance. would really appreciate if someone could help.