2

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

6
  • What is the data type of the price column in your database? Also, you aren't by chance using DataTables for display are you? Commented Jul 15, 2016 at 18:30
  • use dd($service_plans) right after your query and look at the output. If it's sorted there (which I expect it is), you know Laravel is doing things correctly and it's something else which is resorting after. Commented Jul 15, 2016 at 18:32
  • 1
    The order by is working....but it seems that your data is being taken as string instead of int... Commented Jul 15, 2016 at 18:36
  • Yes. I'm using datatable. Commented Jul 15, 2016 at 18:37
  • 3
    If you are using DataTables, it will auto sort on the first column which will make it appear like your orderBy is not working. Commented Jul 15, 2016 at 18:51

4 Answers 4

16

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

Sign up to request clarification or add additional context in comments.

2 Comments

I guess that is the cleanest way according to my scenario.
I'd still probably sort server side still, in the event you ditch datatables or use another solution. ;)
3

You still get that data because of the datatables does ordering your data as well, so your orderBy query isn't work. Just set to your parameter datatable ordering: false to make it works.

$('#datatable').DataTable({
    ordering: false, //disable ordering datatable
});

so, you can sort your data by whatever fields you want althought you dont want to show them in your table.

Comments

-2

You can add a $casts property to your ServicePlan model. This will treat the column as the implicit data type regardless of the database data type.

class ServicePlan extends Model
{
    /**
     * The attributes that should be casted to native types.
     *
     * @var array
     */
    protected $casts = [
        'price' => 'integer',
    ];
}

https://laravel.com/docs/5.2/eloquent-mutators#attribute-casting

4 Comments

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.
makes sense. so this isn't a laravel issue - maybe the laravel tag should be removed?
OP uses Laravel so the tag laravel makes sense. Mike Barwick suggested excellent yajra/laravel-datatables package for Laravel to work with jQuery DataTables.
@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.
-2

Type of price field is string so change it to decimal with 2 numbers after comma.

Run in mysql console:

ALTER TABLE service_plans MODIFY price DECIMAL(10,2);

!!! When making modification on tables on production make sure that You've copy of table before calling ALTER TABLE:

CREATE TABLE service_plans_bak AS SELECT * FROM service_plans;

6 Comments

He's using databtables jQuery plug-in. Which automatically sorts data, regardless of object given (based on first column of datatable).
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 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.
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.
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.