title: Laravel - joining 2 tables with OR operator
body: <p>I have a table called <strong>messages</strong>, and it's columns are</p>
  
  <pre><code>id, sender_id, message, receiver_id, created_at, updated_at
  </code></pre>
  
  <p>And the other table is called <strong>users</strong>, and it's columns are</p>
  
  <pre><code>id, firstname, lastname, email, password, created_at,updated_at
  </code></pre>
  
  <p>I want to get all the messages from all the users, so I'm joining the <em>messages.sender_id and messages.receiver_id with users.id</em></p>
  
  <p>However I don't know how to use "OR" operator while joining in laravel
  here's my raw query that gives me the desired output.</p>
  
  <pre><code>SELECT messages.*,users.* FROM messages JOIN users ON users.id = messages.sender_id 
  OR users.id = messages.receiver_id GROUP BY messages.id
  </code></pre>
  
  <p>Here's the laravel query that I tried</p>
  
  <pre><code>$get_messages = DB::table('messages')
              -&gt;where('messages.sender_id',$user_id)
              -&gt;leftjoin('users', function($join){
                  $join-&gt;on('users.id','=','messages.sender_id'); // i want to join the users table with either of these columns
                  $join-&gt;on('users.id','=','messages.receiver_id');
              })
              -&gt;orwhere('messages.sender_id',$partner_id)
              -&gt;where('messages.receiver_id',$user_id)
              -&gt;orwhere('messages.receiver_id',$partner_id)
              -&gt;groupby('messages.id')
              -&gt;get();
  
  print_r($get_messages);
  </code></pre>