How to get average of column values in laravel

后端 未结 5 470
误落风尘
误落风尘 2020-12-06 17:09

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

5条回答
  •  佛祖请我去吃肉
    2020-12-06 17:37

    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.

提交回复
热议问题