Support the ongoing development of Laravel.io →
Database
Last updated 3 months ago.
0

Try with

$search = DB::table('scores')->where('score', '>', $number)->orderByRaw('RAND()')->first(); 
Last updated 3 months ago.
0

Look for numbers higher then 42, and sort it from low to high with a limit of 1.

Last updated 3 months ago.
0

I tried this solution, but I am getting a weird result. If ever my score is 3, the result will be 30. If the score is 2 the result will be 20 instead of 5 which is the nearest highest score.

Btw, my data is almost consist of 400 scores on my tables.

$secondsearch = DB::table('scores')->where('score', '>=', $score)->orderBy('score', 'ASC')->take(1)->first();
Last updated 3 months ago.
0

assuming you have the model built...

public function nextHighest($number) {
    return Score::orderBy('score', 'asc')->where('score', '>', $number)->firstOrFail();
}

I didn't test that code but I think it should do what you want. Let me know, thanks.

Last updated 3 months ago.
0

Sign in to participate in this thread!

LaraJobs

Your banner here too?

helmikuu27 helmikuu27 helmikuu27 Joined 31 Mar 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

The Laravel portal for problem solving, knowledge sharing and community building.

© 2022 Laravel.io - All rights reserved.