How can i filter the max value of a 'price' field in a different table?
Current code:
$query = Product::join('variants', 'variants.product_id', '=', 'products.id');
$query->select('products.*', DB::raw("MAX(variants.price) as max_variant_price"));
if($params['priceFrom'] !== false) {
$query->whereRaw('max_variant_price >= ' .$params['priceFrom']);
}
if($params['priceTo'] !== false) {
$query->whereRaw('max_variant_price <=' . $params['priceTo']);
}
But getting the following error:
Column not found: 1054 Unknown column 'max_variant_price' in 'where clause'.