Display all users except for the aissigned ones.

Posted 8 months ago by Hash

Hi, 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 shouldnt 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 avalaible users:

User::with('chat_topics')->get() (if userA is assigned to TopicA, so userA shouldnt be displayed in avaialble 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();

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

Reply to

Use Markdown with GitHub-flavored code blocks.