title: Convert mysql raw query contain nested join to laravel eloquent
body: <p>I need to implement this raw query with laravel Eloquent based on table1 model :</p>
  
  <pre><code>SELECT e.* FROM table1 e
  INNER JOIN 
    ( 
      SELECT et.table1_id FROM table1_table2 et
      INNER JOIN table1 e ON e.id = et.table1_id
      INNER JOIN tags t ON t.id = et.tag_id
      WHERE t.id IN(@tags) 
      GROUP BY et.table1_id 
      HAVING COUNT(et.tag_id) = @x
    ) ee ON e.id = ee.table1_id
  JOIN table1_table2 ON table1_table2.table1_id = e.id
  JOIN tags ON tags.id = table1_table2.tag_id 
  GROUP BY table1_table2.table1_id
  </code></pre>
  
  <p><code>@tag</code> var is comma separated integer and <code>@x</code> var is count of <code>@tag</code> var.</p>
  
  <p>there are some ralation between tags table1 and table1_table2 :</p>
  
  <p>In table1 model we have:</p>
  
  <pre><code>public function tags()
  {
      return $this-&gt;belongsToMany(Tag::class, 'table1_table2', 'table1_id', 'tag_id');
  }
  </code></pre>
  
  <p>my larave version is 5.8.35</p>