How do I add on select orderby sorting on search result please?
laravel 5.8?
Advertisement
Answer
OK based on your comment I’ve updated my answer:
on the front end the select menu should update the url with the selected menu item, you can use a onchange to do this:
<select onchange="location = this.value;"> <option value="">Sort By</option> <option value="?sortBy=rate" {{ (request('sortBy') == 'rate' ? 'selected=selected' : '') }}>Rate</option> <option value="?sortBy=popular" {{ (request('sortBy') == 'popular' ? 'selected=selected' : '') }}>Popular</option> <option value="?sortBy=ohirgi" {{ (request('sortBy') == 'ohirgi' ? 'selected=selected' : '') }}>Ohirgi</option> <option value="?sortBy=id" {{ (request('sortBy') == 'id' ? 'selected=selected' : '') }}>id</option> </select>
When selected will add ?sortBy= and the option selected to the url. or ?sortBy=rate
write your query but leave off the ->get() then you can chain additional options on when complete then terminate with ->get() or ->paginate()
//initial query without terminator $doctors = Doctor_list::where('speciality_title', 'LIKE', '%'.$key.'%'); //if there is a sort request use it, otherwise use the default sort switch (request('sortBy')) { case 'rate': $doctors->orderby('rate', 'desc'); break; case 'Popular': $doctors->orderby('popular', 'asc'); break; case 'Ohirgi': $doctors->orderby('ohirgi', 'asc'); break; default: $doctors->orderby('id', 'asc'); break; } //complete the query and terminate it with paginate or ->get() $doctors = $doctors->paginate();