title: Laravel - "WhereHas" query: different for each user
body: <p>Sorry, this is a bit complicated to explain, but basically: The thread table has many messages. A thread has these columns: </p>
  
  <pre><code>id, deleted_at_sender, deleted_at_receiver_ sender_id, receiver_id
  </code></pre>
  
  <p>The messages table has these columns</p>
  
  <pre><code>thread_id, user_id, body
  </code></pre>
  
  <p>With my query, I am trying to get the threads which have not been deleted by the user who the thread was sent to. For instance, if <code>user1</code> sends a message to <code>user2</code>, <code>user2</code> can delete it and he should not be able to see it, while <code>user1</code> should still be able to see it.</p>
  
  <p>I have got that query set up already and it works flawlessly (kind of)</p>
  
  <pre><code>$messages = Message::select('*', \DB::raw('max(created_at) as created_at'))
          -&gt;where($get_user_id, uid())
          -&gt;groupBy('thread_id')
          -&gt;orderBy('created_at', 'desc')
          -&gt;whereHas('thread', function ($q) {
              $q-&gt;whereNull('deleted_at_receiver');
          })
          -&gt;paginate(10);
  </code></pre>
  
  <p>Where is the problem you may ask? It's this line: <code>$q-&gt;whereNull('deleted_at_receiver');</code>. You see, depending on if the current logged in user is the receiver or sender of the mail, I cannot really fill that in. For now, I have sort of hardcoded the <code>deleted_at_receiver</code> column at this line. </p>
  
  <p>I need to somehow figure out a way to  determine if the thread was created by the current logged in user and change <code>$q-&gt;whereNull('deleted_at_receiver');</code> depending on if the user is the sender or receiver.</p>
  
  <p>Any help is highly appreciated. I have spent over 2 days trying to figure this out with no luck. Please help guys.</p>
  
  <p>Thank you.</p>