Unexpected behaviour in laravel 5.3
shenjiayu opened this issue · comments
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.
please see the Talk Example