Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

any way to query specific types of messages #280

Open
keinagae opened this issue Apr 21, 2021 · 2 comments
Open

any way to query specific types of messages #280

keinagae opened this issue Apr 21, 2021 · 2 comments

Comments

@keinagae
Copy link

No description provided.

@tolgatasci
Copy link
Contributor

tolgatasci commented Apr 21, 2021

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

@dhi2
Copy link

dhi2 commented Apr 26, 2021

You can reuse the getConversationMessages function from here:

private function getConversationMessages(Model $participant, $paginationParams, $deleted)

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']
    );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants