Via Collections*
$books = Book::with('ratings')
->get()
->sortBy(function($bk, $key) {
if($bk->rating) {
return $bk->rating->rating;
}
return null;
});
Via Joins
$books = Book::join('rating', 'rating.book_id', '=', 'book.id')
->orderBy('ratings.rating', 'desc')
->select('books.*')
->get();