chat icon indicating copy to clipboard operation
chat copied to clipboard

any way to query specific types of messages

Open keinagae opened this issue 3 years ago • 2 comments

keinagae avatar Apr 21 '21 06:04 keinagae

if you wish use eloquent

Chat::where() or scope https://laravel.com/docs/8.x/eloquent

You can generate queries of your own.

dont forget

https://github.com/musonza/chat#data-transformers

tolgatasci avatar Apr 21 '21 09:04 tolgatasci

You can reuse the getConversationMessages function from here: https://github.com/musonza/chat/blob/a2293493b0a2c6360145c1a0b12e5e1ca7562022/src/Models/Conversation.php#L319

Like this:

$conversation = Chat::conversations()->getById(request('id'));

$tablePrefix = 'chat_';

$paginationParams = [
    'pageName' => 'page',
    'page' => request('page') ? request('page') : 1,
    'perPage' => 10,
    'sorting' => "desc"
];

$message_types = array(); // <- Add message types to this array

$messages = Chat::conversation($conversation)->setParticipant($user)
    ->conversation
    ->messages()
    ->join($tablePrefix.'message_notifications', $tablePrefix.'message_notifications.message_id', '=', $tablePrefix.'messages.id')
    ->where($tablePrefix.'message_notifications.messageable_type', $user->getMorphClass())
    ->where($tablePrefix.'message_notifications.messageable_id', $user->getKey())
    ->when(!empty($message_types), function($query) use ($message_types) {
        $query->whereIn('type', $message_types);
    })
    ->orderBy($tablePrefix.'messages.id', $paginationParams['sorting'])
    ->paginate(
        $paginationParams['perPage'],
        [
            $tablePrefix.'message_notifications.updated_at as read_at',
            $tablePrefix.'message_notifications.deleted_at as deleted_at',
            $tablePrefix.'message_notifications.messageable_id',
            $tablePrefix.'message_notifications.id as notification_id',
            $tablePrefix.'message_notifications.is_seen',
            $tablePrefix.'message_notifications.is_sender',
            $tablePrefix.'messages.*',
        ],
        $paginationParams['pageName'],
        $paginationParams['page']
    );

dhi2 avatar Apr 26 '21 20:04 dhi2