U moet het aggregate
. opgeven functie in het hebben van clausule, kunnen we dezelfde hergebruiken die we hebben in select, zoals deze
$brand = "MAX(CASE WHEN b.attribute_code = 'brand' then b.attribute_value END)";
$model = "MAX(CASE WHEN b.attribute_code = 'model' then b.attribute_value END)";
$category = "MAX(CASE WHEN b.attribute_code = 'category' then b.attribute_value END)";
$subcategory = "MAX(CASE WHEN b.attribute_code = 'subcategory' then b.attribute_value END)";
$brandName = 'honda';
$query = Item::selectRaw("a.number, a.description, {$brand} as brand, {$model} as model, {$category} as category, {$subcategory} as subcategory")
->from('items as a')
->join('attr_maps as b','b.number','=','a.number')
->groupBy('a.number')
->havingRaw("{$brand} = ?", [$brandName])
->orderBy('description')
->paginate(10);
return $query;
BEWERKEN: Na opmerkingen
U kunt voor elke parameter als volgt uitvoeren
$query = Item::selectRaw("a.number, a.description, {$brand} as brand, {$model} as model, {$category} as category, {$subcategory} as subcategory")
->from('items as a')
->join('attr_maps as b','b.number','=','a.number')
->groupBy('a.number')
->orderBy('description');
foreach($param as $key => $value) {
$query = $query->havingRaw("{$$key} = ?", [$value]);
}
$results = $query->paginate(10);
return $results;