title: How to add a sum of amount constraint in Laravel?
body: <p>I have a database with the following Relations: </p>
  
  <p>There are <code>projects</code> - something people can purchase, also has <code>user_id</code>,</p>
  
  <p>There are <code>backers</code> - the people that purchase,</p>
  
  <p>There are <code>orders</code> - each order is a purchase of a <code>Project</code> by <code>Backer</code> (<code>Backer</code> can only purchase each <code>Project</code> once). THere is an amount - showing how much a <code>Backer</code> has paid to purchase the <code>Project</code></p>
  
  <p><a href="https://i.stack.imgur.com/0eqSU.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0eqSU.png" alt="enter image description here"></a></p>
  
  <p>What I need is to write the following query - given the <code>$user_id</code>, <code>$count</code> and <code>$sum</code> find all Backers that have invested at least in <code>$count</code> number of projects with <code>user_id</code> equal to <code>$user_id</code> and having the sum of their purchases for those projects higher then <code>$sum</code>. </p>
  
  <pre><code>  Backer::whereHas('projects', function ($q) use ($sum, $userId){
  
          $q-&gt;where('user_id', $userId);
  
      }, '&gt;=', $leastProjectCount)-&gt;get();
  </code></pre>
  
  <p>but it does not constraint the <code>$sum</code> of purchases</p>
  
  <p><strong>Update:</strong>
  So I came up with this query: </p>
  
  <pre><code>select  * from `backers` \r\n
                      WHERE id &gt; 0 \r\n
                      AND (select count(*)\r\n
                             FROM `projects` \r\n
                             inner join `orders` on `projects`.`id` = `orders`.`project_id` \r\n
                             where `backers`.`id` = `orders`.`backer_id` \r\n
                             and `user_id` = 6 \r\n
                             and exists (\r\n
                                 select * \r\n
                                 from `tags` \r\n
                                 inner join `project_tag` on `tags`.`id` = `project_tag`.`tag_id` \r\n
                                 where `projects`.`id` = `project_tag`.`project_id` \r\n
                                 and `id` in (27)\r\n
                             ) \r\n
                            ) &gt;= 1 \r\n
                      AND (select sum(orders.amount)\r\n
                             FROM `projects` \r\n
                             inner join `orders` on `projects`.`id` = `orders`.`project_id` \r\n
                             where `backers`.`id` = `orders`.`backer_id` \r\n
                             and `user_id` = 6 \r\n
                             and exists (\r\n
                                 select * \r\n
                                 from `tags` \r\n
                                 inner join `project_tag` on `tags`.`id` = `project_tag`.`tag_id` \r\n
                                 where `projects`.`id` = `project_tag`.`project_id` \r\n
                                 and `id` in (27)\r\n
                             ) \r\n
                            ) &gt;= 12
  </code></pre>
  
  <p>note that the subqueries are almost the same, except that the first uses <code>COunt(*)</code> and the second uses <code>sum(orders.amount)</code>. And also note that this query runs if all the parameters were set, but it can be shorter, when some parameters are not set. The problem I am getting is that I don't have pagination with such a query. </p>