Hash

Display all users except for the aissigned ones.

Posted 4 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.

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up

Channels

Reply to

Use Markdown with GitHub-flavored code blocks.