title: Laravel 4: Where Not Exists
body: <p>I need my model to return only those records from one table where a matching record does not exist in another table. I was thinking that the solution might be with <a href="http://laravel.com/docs/eloquent#query-scopes" rel="noreferrer">Query Scopes</a> but the documentation only scratches the surface. So my SQL would look something like this:</p>
  
  <pre><code>SELECT *
  FROM A
  WHERE NOT EXISTS (SELECT A_id FROM B
                  WHERE B.A_id = A.id)
  </code></pre>
  
  <p>Here are my tables:</p>
  
  <pre><code>A
  -------------
  | id | name |
  -------------
  
  B
  --------------------
  | id | A_id | name |
  --------------------
  </code></pre>
  
  <p>Probably unnecessary but here are my models. Model for A:</p>
  
  <pre><code>class A extends Eloquent{
  
      public function B(){
          return $this-&gt;hasOne('B', 'A_id', 'id');
      }
  }
  </code></pre>
  
  <p>Model for B:</p>
  
  <pre><code>class B extends Eloquent{
  
      public function A(){
          return $this-&gt;belongsTo('B', 'A_id', 'id');
      }
  }
  </code></pre>