How to get average of column values in laravel
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.
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();
Try this :
$avgStar = Model::avg('star');
" Model " will replace with your model name