Channel Avatar

Takneeki Code @UCg5_t9uIAUjLfKyGbOE3eeg@youtube.com

None subscribers - no pronouns set

Helping with tutorials. c205f992a1ff982fe20b


Takneeki Code
1 year ago - 2 likes

How to find average and count in Laravel ?

I added a comment Model in my application, and it has a rating and comments section. However, I wanted to display the average rating of a company and the number of comments for that company in the index page.

Here is how I did it, using Eloquent functions withAvg and withCount:

Http\Contollers\CompanyController.php



public function index()
{
$company = company::withAvg('rating as ratings', 'rating')->withCount('comment as comments')
->latest()>filter()->paginate(12);


return view('Company.Record.index', compact('company'));

}