title: Querying laravel many-to-many relathionship
body: <p>I have 3 entities: page, category and page type. 
  Page might be hidden, as well as category. (both have <code>hidden</code> column)
  Page has page_type_id foreign key, so it belongs to page type entity.
  Page and category has many-to-many relationship.</p>
  
  <p>I need to query all pages which are not hidden and which are not linked to categories which are hidden. And page must also has some specific type.</p>
  
  <p>I'm always struggling with Eloquent when it comes to a little bit complicated queries.
  For example, I figured out how to check that a page has specific type:</p>
  
  <pre><code>$materials = Page::whereHas('type', function($query) {
      $query-&gt;where('type', 'material'); // A little bit confusing, type is a column name and also the relationship name, that's why I have type in whereHas and in where. Nevermind
  );
  </code></pre>
  
  <p>I figured out how to eager load categories which are hidden or not using <code>with</code> and subquery.</p>
  
  <p>But how to check that IF page has categories then they must not be hidden and IF page doesn't have categories then it doesn't but I have to get the page anyway?
  <code>whereHas</code> won't load pages which don't have categories. And eager loading just loads or not categories but it has nothing to do with constraining pages based on <code>hidden</code> field in categories</p>