Order By

The orderBy() method sets the ORDER BY clause.

$query->table('movies')->orderBy('rating');
// SELECT * FROM `movies` ORDER BY `movies`.`rating`

You can specify the direction, defaults to ascending:

$query->table('movies')->orderBy('rating', 'desc');
// SELECT * FROM `movies` ORDER BY `movies`.`rating` DESC

You can set multiple columns separated by commas:

$query->table('movies')->orderBy('year, rating');
// SELECT * FROM `movies` ORDER BY `movies`.`year`,`movies`.`rating`

Or call orderBy() more than once:

$query->table('movies')->orderBy('year')->orderBy('rating', 'desc');
// SELECT * FROM `movies` ORDER BY `movies`.`year`,`movies`.`rating` DESC

Specifying the direction

You can set multiple columns specifying a direction for each:

$query->table('movies')->orderBy('year, rating DESC');
// SELECT * FROM `movies` ORDER BY `movies`.`year`,`movies`.`rating` DESC

You can pass in an array:

$query->table('movies')->orderBy(['year desc', 'rating']);
// SELECT * FROM `movies` ORDER BY `movies`.`year` DESC,`movies`.`rating`

Or an associative array where the keys are the column references and the value the direction:

$query->table('movies')->orderBy(['year'=>'desc', 'rating'=>'asc']);
// SELECT * FROM `movies` ORDER BY `movies`.`year` DESC,`movies`.`rating`

Random ordering

Use the orderByRand() method to order the rows at random:

$query->table('movies')->orderByRand();
// SELECT * FROM `movies` ORDER BY RAND()

Note

The orderByRand() method will will probably not work as expected if combined with additional orderBy clauses