Im building blog app and im trying to store average rating in posts table with this function in PostController.php.
public function avgRating ($avgRating) {
$posts=DB::statement('UPDATE posts p
SET p.avgRating=
(SELECT ROUND(AVG(r.rating), 0)
FROM ratings r
WHERE p.id=r.rateable_id)');
$posts = Post::all();
}
Every other function that i created is saving data in database except this one. I tried to run sql query by itself in phpmyadmin and it works, its storing the average rating of each post in that table. Can anyone explain how do i write that function correctly in Laravel?