How to get average of column values in laravel
Try this :
$avgStar = Model::avg('star');
" Model " will replace with your model name
In Laravel how do I get the average of a sum of a value?
With laravel you can use the sum()
method to sum up the values within a collection and the count()
method to count the number elements in the collection. Sum/count is the average:
$sums=$whatever->groupBy('user_id')->selectRaw( "sum( value ) as sum" )->get( "sum" );
$avg=$sums->sum("sum")/$sums->count();
Or you need to use a subquery in sql code.
How to get average of columns in db from laravel
Change whereColumn
to where
and you should be on your way.
whereColumn
is for comparing columns to one another.
Laravel Calculate AVG of a related column
Ok. So this did the job following this blog :
public function avgFeedback() {
return $this->hasMany('App\ClientFeedbackReviews', 'merchant_id', 'id')
->selectRaw('merchant_id,AVG(client_feedback_merchants.stars) AS average_rating')
->groupBy('merchant_id');
}
public function getavgFeedbackAttribute() {
// if relation is not loaded already, let's do it first
if ($this->relationLoaded('commentsCount'))
$this->load('avgFeedback');
$related = $this->getRelation('avgFeedback');
// then return the count directly
return ($related) ? (int) $related->average_rating : 0;
}
and I have used it as follows:
//////// query continued /////////
$getMerchantQuery = $getMerchantQuery->with('avgFeedback');
/////// query continued //////////
get Average of column in one to many Relation in laravel
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.
Laravel average of numbers per group
The simplest would be to select all rows with subject id X, loop through and and add all the scores to a total, then divide that total by the count()
of the rows returned.
A nicer way might be through crafting a cool SQL query that does the same, but that's for you to decide, this is roughly how you do it.
How to get average value of particular user's one column
Try grouping users and get average of the group:
Code looks like:
$data = DB::table('cleaning_scores')
->select(DB::raw('avg(score) as avg, user_id'))
->groupBy('user_id')
->orderByDesc('avg')
->get();
Laravel Getting Average of data of different users using eloquent
Is this what you want? A eloquent query with model?
$data = CleaningScore::select('user_id', \DB::raw('avg(score) as avg'))
->groupBy('user_id')->orderBy('avg','DESC')
->get();
I hope it helps
Related Topics
Change "Billing Details" Text to "Shipping Details" on Woocommerce Checkout Page
PHP MySQL Set Connection Timeout
How to Fix Server Status Code: 302 Found by SQL Inject Me Firefox Addon
How to Create a New Page in Prestashop Admin Panel
Find a Percentage Value in a String Using Preg_Match
PHP MySQL Character Set: Storing HTML of International Content
How to Curry Method Calls in PHP
Nohup on Windows, Exec Without Waiting for Finish
Pdo Mssql Server - Driver Not Found
Codeigniter Command Line Error - PHP Fatal Error: Class 'Ci_Controller' Not Found
Set Multiple='False' in a Form in a Many to Many Relation Symfony2
Set Cart Item Price from a Hidden Input Field Custom Price in Woocommerce 3
How to Check If User Has Already Liked the Facebook Page
In Laravel 5, How to Disable Verifycsrftoken Middleware for Specific Route
How to Send Https Posts Using PHP