title: How to use multiple 'where' clause with multiple tables with Eloquent ORM?
body: <p>I have a database query with multiple 'JOIN' statements in my Laravel application, but I don't know how to correctly add a where clause to it. </p>
  
  <p>Here is my function: </p>
  
  <pre><code>    return Topic::join('blogs', 'topics.blog_id', '=', 'blogs.id')
              -&gt;join('blog_subscriptions as us', function ($j) use ($userId){
                    $j-&gt;on('us.blog_id', '=', 'blogs.id')
                      -&gt;where('us.user_id', '=', $userId);
              })
              -&gt;take(Config::get('topic.topics_per_page'))
              -&gt;offset($offset)
              -&gt;get(['topics.*']);
  </code></pre>
  
  <p>I would like to add a 'where' clause to the  'topics' table - so I add a line where('rating', '>', 1), after "Topic::", so the code is like this: </p>
  
  <pre><code>Topic::where('rating', '&gt;', 1)
              -&gt;join('blogs', 'topics.blog_id', '=', 'blogs.id')
              -&gt;join('blog_subscriptions as us', function ($j) use ($userId){
                  $j-&gt;on('us.blog_id', '=', 'blogs.id')
                  -&gt;where('us.user_id', '=', $userId);
              })
              -&gt;take(Config::get('topic.topics_per_page'))
              -&gt;offset($offset)
              -&gt;get(['topics.*']);
  </code></pre>
  
  <p>but it only leads to an error:</p>
  
  <blockquote>
    <p>SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'rating' in where clause is ambiguous (SQL: select <code>topics</code>.* from <code>topics</code> inner join <code>blogs</code> on <code>topics</code>.<code>blog_id</code> = <code>blogs</code>.<code>id</code> inner join <code>blog_subscriptions</code> as <code>us</code> on <code>us</code>.<code>blog_id</code> = <code>blogs</code>.<code>id</code> and <code>us</code>.<code>user_id</code> = 1 where <code>rating</code> > 1 limit 2 offset 0)</p>
  </blockquote>