Multiple Column Sorting on Laravel 4
Multiple Column Sorting on Laravel 4. In PHP 4 above, we can use direct MySQL queries to sort based on two columns, But we can’t use the direct MySQL Query here. The following query explains you more,
$kv_query = "SELECT * FROM kvcodes_table ORDER BY Name DESC, id ASC"; $kv_result = mysql_query($kv_query);
But the above code does not help you to use in laravel, Here we have function to use and query it. So here in laravel, we have function to use it simply.
orderBy() function simplifies the operations and query writing, The above query will be sorted with the following one,
Kvcodes_table::orderBy('Name', 'DESC') ->orderBy('id', 'ASC') ->get();
That’s it, the above snippet will take care of your multiple sorting operations. Likewise you can add many items into it. so sorting will be narrow down with the multiple sortings.
Else, you have alternative choice, you can use “DB::raw() “. Function to use your custom queries as like the Original SQL query. My next post describe the way to do the Raw mySQL query into it.