title: query relation based on parent column
body: <p>i have 2 tables </p>
  
  <pre><code>accounts  : id , title , disabled , transaction_amount_limit , transaction_count_limit 
  
  account_limits : id , account_id , transaction_amount , transaction_count , date 
  </code></pre>
  
  <p>so each account has bunch of transaction each day ... i want to select the a account that hasn't reached its transactions limit .... current transaction for each account is stored in account_limits table </p>
  
  <p>basically i want to say select account that doesn't have <code>account_limits</code> row or have <code>account_limits</code> but hasn't reached the limits <code>account_limits.transaction_amount &lt; accounts.transaction_amount_limit</code> &amp;&amp; <code>account_limits.transaction_count &lt; accounts.transaction_count_limit</code></p>
  
  <p>something like </p>
  
  <pre><code>select * from `accounts`
  
  (  where not exists (select * from `account_limits` where `accounts`.`id` = `account_limits`.`account_id`)
  OR 
  where exists (select * from `account_limits` where `accounts`.`id` = `account_limits`.`account_id` &amp;&amp;  account_limits.transaction_amount &lt; accounts.transaction_amount_limit  &amp;&amp; account_limits.transaction_count &lt; accounts.transaction_count_limit)
  )
  </code></pre>
  
  <p>i have this so far </p>
  
  <pre><code>    $account = Account::where('disabled' , 0 )-&gt;where(function($q){
          $q-&gt;whereDoesntHave('AccountLimit')-&gt;orWhere('....') ;
      })-&gt;get();
  </code></pre>
  
  <p>as @Flame suggested i tried </p>
  
  <pre><code>Account::whereHas('accountLimits', function($query) {
      $query-&gt;where('account_limits.transaction_amount', '&lt;', 'accounts.transaction_amount_limit')
      -&gt;where('account_limits.transaction_count', '&lt;', 'accounts.transaction_count_limit');
  })-&gt;orHas('accountLimits', '=', 0);
  </code></pre>
  
  <p>the problem is for some reason </p>
  
  <pre><code>where('account_limits.transaction_amount', '&lt;', 'accounts.transaction_amount_limit')
  </code></pre>
  
  <p>in the output will translate to </p>
  
  <pre><code>where `account_limits`.`transaction_amount`  &lt; 'accounts.transaction_amount_limit'
  </code></pre>
  
  <p>and query fails , there's problem with quotations </p>
  
  <pre><code>    this 
  
   'accounts.transaction_amount_limit' 
  
  should be
  
   `accounts`.`transaction_amount_limit`
  </code></pre>