I have a database with the following Relations:
There are projects - something people can purchase, also has user_id,
There are backers - the people that purchase,
There are orders - each order is a purchase of a Project by Backer (Backer can only purchase each Project once). THere is an amount - showing how much a Backer has paid to purchase the Project
What I need is to write the following query - given the $user_id, $count and $sum find all Backers that have invested at least in $count number of projects with user_id equal to $user_id and having the sum of their purchases for those projects higher then $sum.
Backer::whereHas('projects', function ($q) use ($sum, $userId){
$q->where('user_id', $userId);
}, '>=', $leastProjectCount)->get();
but it does not constraint the $sum of purchases
Update: So I came up with this query:
select * from `backers` \r\n
WHERE id > 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
) >= 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
) >= 12
note that the subqueries are almost the same, except that the first uses COunt(*) and the second uses sum(orders.amount). 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.
