I am looking to get the average of several columns on the corresponding model, something like this:
$this->reviews()->avg('communication', 'friendliness')
Where communication and friendliness is an array of column names. However, it seems that aggregated functions only support column names, so I do this:
$attributes = array('communication', 'friendliness'); $score = array(); foreach ($attributes as $attribute) { $score[] = $this->reviews()->avg($attribute); } return round(array_sum($score) / sizeof($attributes), 1);
This leads to several queries. Any suggestions for best practice here?
thanks
source share