Hanie Asemi
Hanie Asemi

Reputation: 1500

get Average of column in one to many Relation in laravel

I Have 2 table:

stores store_feedbacks | store_id , user_id ,rating

rating is number between 1-5 , Now I want to get average of all rating for one store,How I can do it?

Upvotes: 0

Views: 1233

Answers (1)

Joel Hinz
Joel Hinz

Reputation: 25404

Assuming your relationships are correctly set up:

$store->ratings()->avg();

Docs: https://laravel.com/docs/5.7/queries#aggregates - they're from the queries page, but it applies to Eloquent as well.

Upvotes: 3

Related Questions