Here is my query which works as well:
SELECT sum(r.rating) as rank,b.* FROM books as b
LEFT JOIN ranks as r ON b.id = r.book_id
WHERE 1
GROUP BY (b.id)
ORDER BY rank DESC
Now I want to do the same in Laravel. Here is what I've tried:
// Book model
class Book extends Model
{
public function ranks()
{
return $this->hasMany(Rank::class)->sum("rating");
}
}
// Controller
$obj = new Book;
$get = $obj->ranks()->orderBy('rating', 'desc')->get();
It throws this error:
Call to a member function groupBy() on integer
Any idea how can I fix this problem?