How to order result by highest in laravel?

Member

by addison , in category: PHP Frameworks , a year ago

How to order result by highest in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by jasen_gottlieb , a year ago

@addison 

To order results by the highest value in Laravel, you can use the orderBy method in your query builder. Here's an example of how you can do this:

1
2
3
4
$highestResults = DB::table('your_table_name')
    ->select('column_name')
    ->orderBy('column_name', 'desc')
    ->get();


In this example, your_table_name is the name of your database table and column_name is the column by which you want to order the results. By using the orderBy method with the second parameter set to 'desc', you are ordering the results in descending order, with the highest values appearing first.


You can also use the orderBy method on Eloquent models in Laravel:

1
$highestResults = YourModel::orderBy('column_name', 'desc')->get();


Replace YourModel with the name of your Eloquent model class. This will achieve the same result as the query builder example above, but using Eloquent syntax.

Related Threads:

How to hydrate result of query in laravel?
How to do query & show the result in laravel?
How to order by character and number in laravel?
How to order by custom time format in laravel?
How to sort records in alphabetical order in laravel?
How to use model function result in controller by codeigniter?