title: Subqueries in Laravel/Eloquent not working
body: <p>I am currently working on a Lumen based application and I am stuck on an Eloquent/Laravel query. What I am currently working on is this:</p>
  
  <pre><code>$min = $filter['min_spending']*100;
  
  $query = User::isCustomer()
      -&gt;with("interests")
      -&gt;leftJoin("points_history", "points_history.user_id", "=", "users.id")
      -&gt;leftJoin("collections", "collections.user_id", "=", "users.id")
      -&gt;select("users.*",
              DB::raw("(SELECT SUM(amount) FROM points_history
                        WHERE points_history.user_id = users.id
                        AND points_history.confirmed = true ) as total_points"),
              DB::raw("(SELECT SUM(price) FROM collections
                        WHERE paid_at IS NOT NULL 
                        AND refunded_at IS NULL
                        AND collections.user_id = users.id) as total_purchase"),
              DB::raw("(SELECT SUM(collections.price)) as current_purchase"))
              -&gt;where("total_purchase", "&gt;=", $min);
  
  // other relevant code here
  
  $query-&gt;groupBy('users.id')-&gt;orderBy('users.created_at', 'desc');
  </code></pre>
  
  <p>However, when I try to get the result, I get this error:</p>
  
  <pre><code>{"error":"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'total_purchase &gt;=' in 'where clause' (SQL: select count(*) as aggregate from `users` left join `points_history` on `points_history`.`user_id` = `users`.`id` left join `collections` on `collections`.`user_id` = `users`.`id` where `users`.`deleted_at` is null and `role` = 2 and `total_purchase &gt;=` = 200000 group by `users`.`id`)"}
  </code></pre>
  
  <p>The subqueries did not  seem to register which is causing the problem (?) My desired SQL Statement is this:</p>
  
  <pre><code>SELECT users . *, 
                      (SELECT 
                          SUM(amount)
                      FROM
                          points_history
                      WHERE
                          points_history.user_id = users.id
                              AND points_history.confirmed = true)  AS total_points, (SELECT 
                          SUM(price)
                      FROM
                          collections
                      WHERE
                          paid_at IS NOT NULL
                              AND refunded_at IS NULL
                              AND collections.user_id = users.id) AS total_purchase,  SUM(collections.price) AS current_purchase
                      from
                          users
                              left join
                          points_history ON points_history.user_id = users.id
                              left join
                          collections ON collections.user_id = users.id
                      where
                          users.deleted_at is null AND (SELECT 
                                  SUM(price)
                              FROM
                                  collections
                              WHERE
                                  paid_at IS NOT NULL
                                      AND refunded_at IS NULL
                                      AND collections.user_id = users.id) &gt;= 200000
  GROUP BY users.id
  </code></pre>
  
  <p>where 200000 is $min. Could anyone help me pinpoint what the problem is in my query so I can figure out how to fix it? </p>