Display all users except for the aissigned ones.

Published 3 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();

Best Answer (As Selected By Hash)
bobbybouwmann

You should make a diff here for the users. So for example you can fetch the available users and the assigned users and then first the assigned users from the available users or do that in your query

$availableUsers = User::all();

$chatTopic  = ChatTopic::with('users')->first();

$availableUsers = $availableUsers->filter(function ($user) use ($chatTopic) {
    return !$chatTopic->users->contains('id', $user->id);
});

Something like this! Note that this is not tested, but it should put you in the right direction!

Another option is doing something like this

$chatTopic  = ChatTopic::with('users')->first();
$userIds = $chatTopic->users->pluck('id')->toArray();

// Grab all users that are not selected yet
$availableUsers = User::whereNotIn('id, $userIds)->get();
bobbybouwmann

You should make a diff here for the users. So for example you can fetch the available users and the assigned users and then first the assigned users from the available users or do that in your query

$availableUsers = User::all();

$chatTopic  = ChatTopic::with('users')->first();

$availableUsers = $availableUsers->filter(function ($user) use ($chatTopic) {
    return !$chatTopic->users->contains('id', $user->id);
});

Something like this! Note that this is not tested, but it should put you in the right direction!

Another option is doing something like this

$chatTopic  = ChatTopic::with('users')->first();
$userIds = $chatTopic->users->pluck('id')->toArray();

// Grab all users that are not selected yet
$availableUsers = User::whereNotIn('id, $userIds)->get();
Hash

That still displays duplicate users,

TOPIC A EDIT SCREEN

Assigned Users user a user b

Available Users user a user b

I want something like this:

TOPIC A EDIT SCREEN

Assigned Users user a user b

Available Users

TOPIC B EDIT SCREEN

Assigned Users

Available Users user a user b

Hash

no one???

Snapey
Snapey
3 months ago (998,475 XP)

Bobby's second example seems good to me

Instead of moaning, show what code you have

Hash

@Snapey this is what i have: the above code works really well its just not what i want as it duplicates.

         public function get_users()
        {
           $chatTopic  = ChatTopic::with('users')->first();
             $userIds = $chatTopic->users->pluck('id')->toArray();


               return  $availableUsers = User::whereNotIn('id', $userIds)->get();
        }

public function getAssignedusers($topic_id)
{
    return UserList::collection(User::has('chat_topics')
        ->whereHas('chat_topics', function ($query) use ($topic_id) {
            $query->where('id', $topic_id);
        })->get());
}

it displays something this:

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.

Snapey
Snapey
3 months ago (998,475 XP)

you got it working now?

Hash

Yeap, what @bobbybouwmann posted, directed me to a proper way, many thanks to him :)

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