I'm working with Laravel 9 for a forum project and I wanted to return all the questions based on votes
of that question.
In fact the questions
table has two fields named upvotes
& downvotes
showing the votes of the question (just like stackoverflow):
Now in order to get the questions with the most upvotes, I tried this:
public function theMostUpVotes()
{
$questions = Question::orderBy('upvotes')->get();
}
But it does not work out since the questions with high votes do not show up in the collection and all the questions are shown instead.
So how to fix this issue and show the questions with the most up votes?
I would really appreciate any idea or suggestion from you guys about this...
Since orderBy
by default ascending order
so you have to specify direction as second param
$questions = Question::orderBy('upvotes','desc')->get();
or use orderByDesc
$questions = Question::orderByDesc('upvotes')->get();