Musíte zadat aggregate
funkce v mít klauzuli, můžeme znovu použít stejnou, jakou máme v select takto
$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;
UPRAVIT: Po komentářích
Můžete provést pro každý parametr takto
$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;