orderBy in Laravel + jquery datatables
Since you're using Datatables jQuery plug-in, remove the orderBy
in your controller query.
Instead, sort via datatables (which will do so by default):
$('#datatable').DataTable({
"order": [[ 2, "asc" ]] // Order on init. # is the column, starting at 0
});
There's also a nice little Laravel package for datatables that will set the data. Check it and see if it'd be useful: https://github.com/yajra/laravel-datatables
code-8
I'm B, I'm a cyb3r-full-stack-web-developer. I love anything that is related to web design/development/security, and I've been in the field for about ~9+ years. I do freelance on the side, if you need a web project done, message me. ;)
Updated on July 09, 2022Comments
-
code-8 almost 2 years
In my database, here is what I have
I've tried to query everything and order by price
$service_plans = DB::table('service_plans') ->orderBy('price', 'asc') ->get();
I kept getting
Did I miss anything or did anything that I'm not suppose to here ?
Any hints ?
P.S. Keep in mind that I'm using jQuery DataTables
-
Mike Barwick almost 8 yearsHe's using databtables jQuery plug-in. Which automatically sorts data, regardless of object given (based on first column of datatable).
-
Gyrocode.com almost 8 yearsThe problem is not in the data type of the price field. jQuery DataTables sorts table by first column by default. See answer by Mike Barwick for more explanation.
-
Gyrocode.com almost 8 yearsThe problem is not in the data type of the price field. jQuery DataTables sorts table by first column by default. See answer by Mike Barwick for more explanation.
-
swatkins almost 8 yearsmakes sense. so this isn't a laravel issue - maybe the laravel tag should be removed?
-
num8er almost 8 years@Gyrocode.com look at title: "orderBy in Laravel" so author tells that he has problems with sorting and in question author did not defined that he has jQuery-Datatables (there is only tag: datatables), but another question is: what if the customer says "I don't need 200 records in one page", so he will return to idea that serverside must return already sorted and paginated data.
-
Gyrocode.com almost 8 yearsI agree the question is vague, but there is
datatables
tag and OP confirms that he's using jQuery DataTables in comments. With jQuery DataTables sorting is either done client-side or server-side but not in the way OP does it. -
num8er almost 8 years@Gyrocode.com ok. so made changes in question to not to desinform users.
-
Gyrocode.com almost 8 yearsOP uses Laravel so the tag
laravel
makes sense. Mike Barwick suggested excellent yajra/laravel-datatables package for Laravel to work with jQuery DataTables. -
code-8 almost 8 yearsI guess that is the cleanest way according to my scenario.
-
Mike Barwick almost 8 yearsI'd still probably sort server side still, in the event you ditch datatables or use another solution. ;)
-
user1669496 almost 8 yearsI believe this is the correct answer or should be done regardless. Assuming datatables stops being used at some point, this issue will present itself again. No reason whatsoever to downvote this answer.
-
swatkins almost 8 years@Gyrocode.com Sure the OP uses Laravel, yet this issue is sure to come up for someone using CodeIgniter, or Rails, or Python. The way this question is titled and tagged, we're not helping those developers.