'How can i get the highest score from laravel query

My table as below

enter image description here

How i want to show data from

name score

Riyal 17

demo2 11

demo3 9

demo1 1

I want to show from higest to low and will show only 10 data from table.

my code is

 public function index()
{
    $score_board = ScoreBoard::orderBy('id')->max('score');
    return new ScoreBoardResource($score_board);
}

but it gives me nothing. Any idea how can i do it



Solution 1:[1]

->max('score') would return 17... If you want 10 records, sorted highest to lowest, you need to use the proper methods:

$scoreBoard = ScoreBoard::orderBy('score', 'DESC')->limit(10)->get();

Please read the Documentation:

https://laravel.com/docs/8.x/queries#ordering-grouping-limit-and-offset

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tim Lewis