I don't know if I expressed the issue clearly, so I'll try again.
I have products that have specific combinations when they should be assigned to a client. I find the correct product by checking all combination options against themselves - first, I get the number of matching options which I pass via the $options array, and then I check if that number is equal to all options for that particular combination. If these numbers match, then combination matches, therefore product matches.
What I'm trying to do is compare relation count twice - once that matches subquery, and once with total. I got it to work by passing a subquery that I copied from the final query, but I want to get it to work using Laravel's query builder.
Here's what I got going right now:
public function scopeMatching($query, $options)
{
return $query->whereHas('combinations', function ($combos) use ($options) {
$matchingQuery = "select count(*) from `quiz_options`
inner join `product_combination_options` on `quiz_options`.`id` = `product_combination_options`.`option_id`
where `product_combinations`.`id` = `product_combination_options`.`combination_id`
and `id` in ({$options->implode(',')})";
$totalQuery = "select count(*) from `quiz_options`
inner join `product_combination_options` on `quiz_options`.`id` = `product_combination_options`.`option_id`
where `product_combinations`.`id` = `product_combination_options`.`combination_id`";
return $combos->whereRaw("({$matchingQuery}) = ({$totalQuery})");
});
}
Notice that $matchingQuery is exactly the same as $totalQuery, it only has additional "and id in..."