skip to Main Content

I have a column of type “varchar” but it consists of integer.

When I make a query with orderBy, it results like below.

10
11
12
9

I have implemented orderBy like below:

->orderBy("stdmap.prvid");

When I run query directly in phpmyadmin with order stdmap.prvid+0, it is giving me correct result. But I am not sure how to add +0 in laravel query.

3

Answers


  1. Try this:

    ->orderBy(DB::raw("stdmap.prvid+0"));
    

    It should work.

    Login or Signup to reply.
  2. How about with orderByRaw?

    ->orderByRaw('stdmap.prvid+0 asc')
    
    Login or Signup to reply.
  3. Order the resulting collection

    $unorderedThings = Thing::orderBy('id')->get();
    $orderedThings = $unorderedThings->sort();
    

    Hope this helps

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search