how to join two tables using eloquent or query builder

I have this query which basically lists topics that two users have in common.

$subcommon= SubjectUser::selectRaw('topic_id, count(topic_id) AS aggregate')
     ->whereIn('user_id', [4, 2])->groupBy('topic_id')

For example the query result for the table below would be



 1|2      |3
 2|4      |3
 3|5      |1

I have another table(tabletwo) that also has topic_id which I would like to join so that I get the query result of row 2 from the second table. How would I go about doing this?


 1|6      |2
 2|7      |3
 3|7      |1

Source: laravel

Leave a Reply