Lets say I have this column
star
----
1
3
3
1
2
5
3
It has seven rows and there are integer values! I want to have
If you have more columns you can use the native function of the database manager using DB::raw
$products = DB::table('products')
->select('id','name',DB::raw('round(AVG(quantity),0) as quantity'))
->groupBy('id','name')
->get();
// let's say the Model name is Review and Table name is reviews, you have a 'star' column on it
$value = Reviews::all();
$value->avg('star');
return view('page.name')->withValue($value);
// it will calculate an average of star column
If you want to do it with the query builder you can use aggregate methods like avg
:
$avg_stars = DB::table('your_table')
->avg('star');
Laravel 5 docs about aggregates.
Table::Where('column', $case)->pluck('column')->avg()
Try this :
$avgStar = Model::avg('star');
" Model " will replace with your model name