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

Unexpected behaviour in laravel 5.3 #114

Open
shenjiayu opened this issue Feb 28, 2018 · 1 comment
Open

Unexpected behaviour in laravel 5.3 #114

shenjiayu opened this issue Feb 28, 2018 · 1 comment

Comments

@shenjiayu
Copy link
Contributor

shenjiayu commented Feb 28, 2018

First of all, thanks for creating such a nice package.

I have come across an unexpected behaviour in Laravel 5.3.
FYI: I am using latest release of this package

Only can sender load messages for a conversation from the db, the receiver cannot load messages from the db so I dig into the source code.
Found that the getMessagesById function in ConversationRepository.php file has the following query for the eager loading relations of messages.

return Conversation::with(['messages' => function ($query) use ($userId, $offset, $take) {
            $query->where(function ($qr) use ($userId) {
                $qr->where('user_id', '=', $userId)
                    ->where('deleted_from_sender', 0);
            })
            ->orWhere(function ($q) use ($userId) {
                $q->where('user_id', '!=', $userId)
                    ->where('deleted_from_receiver', 0);
            });

            $query->offset($offset)->take($take);

        }])->with(['userone', 'usertwo'])->find($conversationId);

which translates to
select * from messages where conversation_id in ($conversationId) and (user_id = $userId and deleted_from_sender = 0) or (user_id != $userId and deleted_from_receiver = 0).

Actually the query should look like this
select * from messages where conversation_id in ($conversationId) and ((user_id = $userId and deleted_from_sender = 0) or (user_id != $userId and deleted_from_receiver = 0))

and the code for the passed callback function for the message relations do the trick

$query->where(function ($qr) use ($userId) {
        $qr->where(function ($q) use ($userId) {
            $q->where('user_id', '=', $userId)
                ->where('deleted_from_sender', 0);
        })
        ->orWhere(function ($q) use ($userId) {
            $q->where('user_id', '!=', $userId)
                ->where('deleted_from_receiver', 0);
        });
    });
});

I don't know if it was the issue of Laravel 5.3. Please review. Thanks.

@shenjiayu shenjiayu reopened this Apr 9, 2018
@nahid
Copy link
Owner

nahid commented Jul 2, 2018

please see the Talk Example

https://github.com/nahid/talk-example

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

2 participants