LiamMcArthur
3 months ago

Multiple GroupBy across multiple relationships

Posted 3 months ago by LiamMcArthur

I'm working on a website that sells new vehicles that are imported via an external feed. We've realised that we'd like to remove duplicate vehicles that have the exact same features (such as model, range, year, weight). In order to achieve this, I figured we'd make use of GroupBy when querying our data, but unfortunately I haven't been able to get it working correctly.

Here's what I have so far. I'm trying to get unique titles of the model and range:

$query->whereHas('model', function($q) {
    $q->whereHas('range', function($q2) {
        $q2->groupBy('title');
    });
    $q->groupBy('title');
});

Is there a correct practise of sub-querying out such data using groupBy?

Please sign in or create an account to participate in this conversation.