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);
});
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community