title: bind parameter in whereraw query with join in laravel
body: <p>below is my code to join two tables and get the latest messages of a particular user_id but i am not able to pass parameter into whereRaw clause. i have tried several ways but no outcome.</p>
  <pre><code>$id = $request-&gt;user_id;
  
  $chats = DB::table('users')
      -&gt;leftJoin('messages', function($query)
      {
          $query-&gt;on('users.id', '=', 'messages.to_id')
          -&gt;whereRaw('messages.id IN (select MAX(messages.id) from messages join users on users.id = messages.to_id where messages.to_id = ? group by users.id)',[$id]);
      })
      -&gt;select('users.id as user_id', 'users.name', DB::raw('CONCAT(&quot;https://www.interwebs.co.in/puzzle/attach/&quot;, users.avatar) AS image') , 'users.mobile', 'messages.id', 'messages.from_id', 'messages.to_id', 'messages.body as message', 'messages.attachment', 'messages.seen as seen_count', 'messages.created_at')
      -&gt;where('messages.from_id', $request-&gt;user_id)
      -&gt;orWhere('messages.to_id', $request-&gt;user_id)
      -&gt;get();
  </code></pre>
  <p>it is just showing me the same error every time that undefined variable $id.</p>