If you want to alternate when clicking the same button, you'd need to store it in session or something, so that when they click it you read from session in the controller to see what it was last. If it was "desc", then sort by "asc" and set it in session. Next time it's clicked, read from session again. If it's "asc" sort by "desc" and set it in session, etc.
May 26, 2018
5
Level 1
Toggle between ASC and DESC ordering
Hello everyone,
I'm new in Laravel and I'm stuck while trying to order some data by column. So, I have this situation:
-I've made button inside form:
form method="GET" action="/sortByMovies"> button class="btn btn-default" type="submit"> /form>
-Also, I've route:
Route::get('/sortByMovies', 'MovieController@sortByMovies');
-And, finally, function looks like:
$movies = Movie::orderBy('movie_name')->get();
return view('movies.index', compact('movies'));
-And, this works fine, but when I click on the button again, I want to order in descending way, ie. I want this button to toggle between asc and desc. I tried a lot of things, but unsuccessfully.
Please or to participate in this conversation.