title: Laravel query where column value is greater than sum of related model column values
body: <p>I have two tables transactions and transaction_allocations. One transaction can have many allocations.</p>
  <p><strong>Transaction Model</strong></p>
  <pre><code>Schema::create('transactions', function (Blueprint $table) {
              $table-&gt;bigIncrements('id');
              $table-&gt;timestamps();
              $table-&gt;foreignId('contact_id')-&gt;constrained();
              $table-&gt;decimal('amount',19,4)-&gt;nullable();
          });
  </code></pre>
  <p><strong>Allocation Model</strong></p>
  <pre><code>Schema::create('transaction_allocations', function (Blueprint $table) {
              $table-&gt;bigIncrements('id');
              $table-&gt;timestamp('created_at');
              $table-&gt;foreignId('transaction_id')-&gt;nullable()-&gt;constrained();
              $table-&gt;foreignId('bill_id')-&gt;nullable()-&gt;references('id')-&gt;on('bills');
              $table-&gt;decimal('amount',19,4)-&gt;nullable();
          });
  </code></pre>
  <p><strong>Relationship in Transaction Model</strong></p>
  <pre><code>public function allocations(){
          return $this-&gt;hasMany(TransactionAllocation::class);
      }
  </code></pre>
  <p>I need to query all transactions where the transaction amount is greater than the sum of all allocated amounts for that transaction. (Basically finding transaction which has unallocated balances).</p>
  <pre><code>Transaction::where('contact_id',$id)-&gt;where('amount','&gt;',sum of allocations)-&gt;get();
  </code></pre>
  <p>How do I achieve it?</p>
  <p>I was able to create an accessor to do this calculation and find an unallocated amount. But seems like accessors cannot be used in where. I don't want to load all transactions and then filter it as it will be too much.</p>
  <p>I want to query and get filtered lines directly. How do I do it?</p>