Trying to group by id using query builder, not working

36 Views Asked by At

I have the following function that fetches the products of pharmacies that are close to the user's location

public static function productsByDistance($latitude, $longitude, $radius = 10, $search = '', $paginated = false, $take = 30, $random = false, $groupByPharmacy = false)
    {
        $nearestOffices = DB::table('office_has_products')
            ->join('offices', 'office_has_products.office_id', '=', 'offices.id')
            ->select('office_has_products.product_id')
            ->selectRaw('MIN(
        ST_Distance_Sphere(
            point(offices.office_longitude, offices.office_latitude),
            point(?, ?)
        ) / 1000) as distance',  // Divide by 1000 to convert meters to kilometers
                [$longitude, $latitude])
            ->groupBy('office_has_products.product_id');

        // Main query
        $products = DB::table('products')
            ->joinSub($nearestOffices, 'nearest_offices', 'products.id', '=', 'nearest_offices.product_id')
            ->join('office_has_products', 'products.id', '=', 'office_has_products.product_id')
            ->join('offices', 'office_has_products.office_id', '=', 'offices.id')
            ->join('pharmacies', 'offices.pharmacy_id', '=', 'pharmacies.id')
            ->select('products.*', 'pharmacies.pharmacy_name', 'pharmacies.slug', 'pharmacies.logo', 'nearest_offices.distance')
            ->where('products.name', 'like', '%' . $search . '%')
            ->where('pharmacies.status', Pharmacy::STATUS_APPROVED)
            ->whereNull('pharmacies.deleted_at')
            ->where('products.is_active', true)
            ->where('nearest_offices.distance', '<=', $radius)
            ->distinct('products.id')
            ->orderBy('nearest_offices.distance');


        if ($random) {
            $products->inRandomOrder();
        }

        if ($paginated) {
            return $products->paginate(30);
        }

        return $products->take($take)
            ->get();
    }

However, If I try to add groupBy('pharmacy_id') I get the error:

SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'farmalinkcr.products.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

If I disable strict mode in config/database.php I get only one product grouped, for context, I have around 200 products for the same pharmacy, so I should get the pharmacy_id as key and inside The array the 200 products, but I only get one.

What I'm really trying to do aside from grouping by pharmacy_id is also limiting the amount of products for each pharmacy, as a pharmacy that has 200+ products can shadow smaller pharmacies

0

There are 0 best solutions below