0

I want to fetch data with messages sent with reply towards me as login user or messages sent to me:

return self::where('sender', '=', 'user1')
    ->whereIn('id', function($query) {
        $query->select(DB::raw(1))
        ->from('replies')
        ->whereRaw('replies.messages_id = id AND replies.receiver = ?', array('user1'));
    })
    ->orWhere('receiver', '=', 'user1')
    ->orderBy('created_at', 'desc')->get();

This works only when user1 is receiver inside messages table, but when he is sender and that message has replies inside replies table I want to get it only if has at least one reply with receiver as user1 or me as logged user. If I send a message and reply to it without receiver replying to it I want to skip it.

In this part I will describe what am I trying to do:

self::where('sender', '=', 'user1')
        ->whereIn('id', function($query) {
            $query->select(DB::raw(1))
            ->from('replies')
            ->whereRaw('replies.messages_id = id AND replies.receiver = ?', array('user1'));
        })

First use where to get messages with me as sender, then use whereIn to check are ids from previous where inside replies table in column messages_id and if they are I am trying to say it needs also to have me as receiver and if does to get it back.

1 Answers1

0

I just changed from whereIn to whereExists and it works as it should :D

  • Okay, I will! Check up my new question :D https://stackoverflow.com/questions/51725788/get-onlytrashed-does-not-exist-in-query-builder –  Aug 07 '18 at 11:36