How to order result by highest in laravel?

Member

by addison , in category: PHP Frameworks , 19 days ago

How to order result by highest in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by jasen_gottlieb , 19 days 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.