Support the ongoing development of Laravel.io →
Cache Database Eloquent
Last updated 1 year ago.
0

If you look into Mysql documentation you will find out that you can use RAND() function with a "seed" which is a number. By using the same seed you will always get the same results that are randomised.

In example below you will get the same cached random result with pagination. Here randomisation changes every 5 minutes.

$page = $request->page;
$questions = Cache::remember('questions_'.$page, 5 * 60, function() use($ads) {
            $seed = rand(1,9999);
            return Questions::all()->orderBy(DB::raw('RAND('.$seed.')'))->paginate(10);
        });
Last updated 7 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Infine2014 infine2014 Joined 22 Mar 2014

Moderators

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

Your logo here?

Laravel.io

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

© 2024 Laravel.io - All rights reserved.