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 it added and divided by the rows there are.
How do I do it in laravel. I can do it in plain php but I want to learn it in laravel.
Try this :
$avgStar = Model::avg('star');
" Model " will replace with your model name
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 love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With