How to order result by highest in laravel?


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


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:

$highestResults = DB::table('your_table_name')
    ->orderBy('column_name', 'desc')

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:

$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.