orderBy in Laravel + jquery datatables

14,123

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

Share:
14,123
code-8
Author by

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, 2022

Comments

  • code-8
    code-8 almost 2 years

    In my database, here is what I have

    enter image description here

    I've tried to query everything and order by price

    $service_plans = DB::table('service_plans') ->orderBy('price', 'asc') ->get();
    

    I kept getting

    enter image description here

    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
    Mike Barwick almost 8 years
    He's using databtables jQuery plug-in. Which automatically sorts data, regardless of object given (based on first column of datatable).
  • Gyrocode.com
    Gyrocode.com almost 8 years
    The 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
    Gyrocode.com almost 8 years
    The 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
    swatkins almost 8 years
    makes sense. so this isn't a laravel issue - maybe the laravel tag should be removed?
  • num8er
    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
    Gyrocode.com almost 8 years
    I 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
    num8er almost 8 years
    @Gyrocode.com ok. so made changes in question to not to desinform users.
  • Gyrocode.com
    Gyrocode.com almost 8 years
    OP 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
    code-8 almost 8 years
    I guess that is the cleanest way according to my scenario.
  • Mike Barwick
    Mike Barwick almost 8 years
    I'd still probably sort server side still, in the event you ditch datatables or use another solution. ;)
  • user1669496
    user1669496 almost 8 years
    I 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
    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.