Search…
Order By
The orderBy method seems simple but has a lot of depth depending on the type of arguments you pass in.
Calling orderBy multiple times appends to the order list.

Order By (String)

Name
Type
Required
Default
Description
column
any
true
The name of the column to order by. An Expression can be passed as well.
direction
string
false
"asc"
The direction by which to order the query. Accepts "asc"or "desc".
QueryBuilder
1
query.from( "users" )
2
.orderBy( "email" );
Copied!
MySQL
1
SELECT *
2
FROM `users`
3
ORDER BY `email` ASC
Copied!
Calling orderBy multiple times will append to the order list.
QueryBuilder
1
query.from( "users" )
2
.orderBy( "email" )
3
.orderBy( "username", "desc" );
Copied!
MySQL
1
SELECT *
2
FROM `users`
3
ORDER BY
4
`email` ASC,
5
`username` DESC
Copied!
You can also provide an Expression.
QueryBuilder
1
query.from( "users" )
2
.orderBy( query.raw( "DATE(created_at)" ) );
Copied!
MySQL
1
SELECT *
2
FROM `users`
3
ORDER BY DATE(created_at)
Copied!

Order By (List)

Name
Type
Required
Default
Description
column
any
true
The list of the columns to order by. Each column can optionally declare it's sort direction after a pipe delimiter. (e.g. `"height
desc"`).
direction
string
false
"asc"
The direction by which to order the query. Accepts "asc"or "desc". This value will be used as the default value for all entries in the column list that fail to specify a direction for a specific column.
QueryBuilder
1
query.from( "users" )
2
.orderBy( "email|asc,username", "desc" );
Copied!
MySQL
1
SELECT *
2
FROM `users`
3
ORDER BY
4
`email` ASC,
5
`username` DESC
Copied!

Order By (Array of Strings)

Name
Type
Required
Default
Description
column
any
true
The array of the columns to order by. Each column can optionally declare it's sort direction after a pipe delimiter. (e.g. `"height
desc"`).
direction
string
false
"asc"
The direction by which to order the query. Accepts "asc"or "desc". This value will be used as the default value for all entries in the column array that fail to specify a direction for a specific column.
QueryBuilder
1
query.from( "users" )
2
.orderBy( [ "email|asc", "username" ], "desc" );
Copied!
MySQL
1
SELECT *
2
FROM `users`
3
ORDER BY
4
`email` ASC,
5
`username` DESC
Copied!

Order By (Array of Structs)

Name
Type
Required
Default
Description
column
any
true
The array of the columns to order by. Each column can optionally declare it's sort direction using a struct. The struct should have a column key and an optional direction key. (e.g. { column = "favorite_color", direction = "desc" }).
direction
string
false
"asc"
The direction by which to order the query. Accepts "asc"or "desc". This value will be used as the default value for all entries in the column array that fail to specify a direction for a specific column.
QueryBuilder
1
query.from( "users" )
2
.orderBy( [
3
{ "column": "email", "direction": "asc" },
4
"username"
5
], "desc" );
Copied!
MySQL
1
SELECT *
2
FROM `users`
3
ORDER BY
4
`email` ASC,
5
`username` DESC
Copied!

Order By (Subquery)

Name
Type
Required
Default
Description
column
any
true
The name of the column to order by. An Expression can be passed as well. An array can be passed with any combination of simple values, array, struct, or list for each entry in the array (an example with all possible value styles: column = [ "last_name", [ "age", "desc" ], { column = "favorite_color", direction = "desc" }, "height|desc" ];. The column argument can also just accept a comman delimited list with a pipe ( | ) as the secondary delimiter denoting the direction of the order by. The pipe delimiter is also used when parsing the column argument when it is passed as an array and the entry in the array is a pipe delimited string.
direction
string
false
"asc"
Ignored when using a Function or QueryBuilder instance.
You can order with a subquery using either a function or a QueryBuilder instance.
QueryBuilder
1
query.from( "users" )
2
.orderBy( function( q ) {
3
q.selectRaw( "MAX(created_date)" )
4
.from( "logins" )
5
.whereColumn( "users.id", "logins.user_id" );
6
} );
Copied!
MySQL
1
SELECT *
2
FROM `users`
3
ORDER BY (
4
SELECT MAX(created_date)
5
FROM `logins`
6
WHERE `users`.`id` = `logins`.`user_id`
7
)
Copied!

Order By Raw

Name
Type
Required
Default
Description
expression
string
true
The raw SQL expression to use.
bindings
array
false
[]
Any bindings (?) used in the expression.
QueryBuilder
1
query.from( "users" )
2
.orderByRaw( "CASE WHEN status = ? THEN 1 ELSE 0 END DESC", [ 1 ] );
Copied!
MySQL
1
SELECT *
2
FROM `users`
3
ORDER BY CASE WHEN status = ? THEN 1 ELSE 0 END DESC
Copied!

clearOrders

Name
Type
Required
Default
Description
No arguments
Clears the currently configured orders for the query. Usually used by downstream libraries like Quick.
QueryBuilder
1
query.from( "users" )
2
.orderBy( "email" )
3
.clearOrders();
Copied!
MySQL
1
SELECT *
2
FROM `users`
Copied!

reorder

Name
Type
Required
Default
Description
column
any
true
The name of the column to order by. An Expression can be passed as well.
direction
string
false
"asc"
The direction by which to order the query. Accepts "asc"or "desc".
Clears the currently configured orders for the query and sets the new orders passed in. Any valid argument to orderBy can be passed here. Usually used by downstream libraries like Quick.
QueryBuilder
1
query.from( "users" )
2
.orderBy( "email" )
3
.reorder( "username" );
Copied!
MySQL
1
SELECT *
2
FROM `users`
3
ORDER BY `username` ASC
Copied!
Last modified 1yr ago