title: Laravel Non-grouping field 'net_total' is used in HAVING clause
body: <p>I have written a query that worked fine in the local server. But in production, it is not working. Here is the query.</p>
  <pre><code>Invoice::with('contact_fk','transactions')
      -&gt;where('date','&lt;=',$this-&gt;query_date)
      -&gt;withSum('transactions', 'amount')
      -&gt;havingRaw('COALESCE(transactions_sum_amount,0) &lt; invoices.net_total')
      -&gt;orderBy('due_date','asc');
  </code></pre>
  <p>The error is as follows:</p>
  <pre><code>SQLSTATE[42000]: Syntax error or access violation: 1463 Non-grouping field
   'net_total' is used in HAVING clause (SQL: select sum(`net_total`) as 
   aggregate from (select `invoices`.*, (select sum(`transaction_allocations`.`amount`)
    from `transaction_allocations` where `invoices`.`id` = `transaction_allocations`.`doc_id` 
    and `transaction_allocations`.`doc_type` = App\Models\Invoice and
     `transaction_allocations`.`deleted_at` is null) as `transactions_sum_amount` from 
     `invoices` where `date` &lt;= 2021-07-27 having COALESCE(transactions_sum_amount,0) &lt; net_total)
      as `temp_table`)
  </code></pre>
  <p>A lot of suggestions online revolve around making the <code>'strict' =&gt; false</code> which is not acceptable as it avoids the issue and does not solve it.</p>
  <p>How can I solve it? My requirement is to bring all invoices which are unpaid. Payment is stored in a separate table called <code>transactions</code>. So all invoices where <code>net_total</code> of the invoice is greater than the sum of <code>amount</code> of transactions for this invoice.</p>
  <p>As requested including the two table structure with relevant fields. They have a polymorphic relationship. The <code>transaction_allocation</code> table contains <code>doc_id</code> and <code>doc_type</code> fields which are filled as <code>Apps\Models\Invoice</code> and <code>invoice_id</code> respectively. The name of the relationship is <code>transactions</code>.</p>
  <pre><code>Schema::create('invoices', function (Blueprint $table) {
      $table-&gt;bigIncrements('id');
      $table-&gt;timestamps(); 
      $table-&gt;decimal('net_total',19,4);
  });
  
  Schema::create('transaction_allocations', function (Blueprint $table) {
      $table-&gt;bigIncrements('id');
      $table-&gt;foreignId('transaction_id')-&gt;nullable()-&gt;constrained();
      $table-&gt;Nullablemorphs('doc');
      $table-&gt;decimal('amount',19,4)-&gt;nullable();
  </code></pre>
  <p>});</p>