Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Ace's avatar
Level 7

OrderBy /SortBy tabular data with the Query Builder

How can I sortyBy with the query builder? on Laravel 4.2 $sortBy = Request::get('sortBy'); http://laravel.io/bin/2WGxj

the orderBy is not Added to the query.

yes I need to upgrade my Laravel ASAP :)

0 likes
1 reply
willvincent's avatar
Level 54

what's the value of sortBy? Is it passing in the column to sort by? just a boolean? what?

You should probably either have it pass in the column (maybe not the most secure option), or determine the column before building the query.. in either case you could then just do something like this:

$direction = 'DESC';
$column = 'saleDate';

if (Request::get('sortBy')) {
  $column = 'trader_id';
}

$data = table('foo')
          ->orderBy($column, $direction)
          ->get();
1 like

Please or to participate in this conversation.