Skip to content

Get all participant Models that a Model ever had a conversation with #304

@mooxnl

Description

@mooxnl

I'm trying to retrieve a list of all the Models (participants) that a certain Model ever had a conversation with.

My chat screen has 3 columns:

  1. list of contacts (grouped, so each contact is displayed only once and sorted by latest message first)
  2. list of conversations with the selected contact
  3. messages inside the selected conversation

My question is regarding the first column. Is this possible?

I tried it with this query to get all participants for User with id 1, which works, but doesn't group the participants:

Musonza\Chat\Models\Conversation::query()
    ->whereHas('participants', function($query) {
        $query->whereHasMorph(
            'messageable',
            [User::class],
            function ($query) {
                $query->where('id', 1);
            })
            ->groupBy(['messageable_type', 'messageable_id']);
    })
    ->get()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions