Right so on my website, registered users can create posts and like other peoples posts. When displaying all the posts i have options where the users can sort by title, date and like count. I have done the title and date but im having trouble with sorting by like count. What would be the best way to implement this feature into my system.
This is my post controller method for retrieving and ordering the posts:
public function getEvents(Request $request){
if($request['sort'] == "created_at"){
$posts = Post::orderBy('created_at', 'desc')->get();
}
elseif($request['sort'] == "title"){
$posts = Post::orderBy('title', 'desc')->get();
}
elseif($request['sort'] == "like"){
//how would I go about ordering by like count here
}
return view ('eventspage', ['posts' => $posts]);
}
Here is my view where users select what to order by:
<form action="{{ route('events') }}">
<select name="sort" onchange="this.form.submit()" class="form-control">
<option value="">Sort By</option>
<option value="created_at">Date</option>
<option value="title">Title</option>
<option value="like">Likes</option>
<input type="hidden" name="formName" value="sort">
</select>
</form>