Is there any way to fetch a random row from the table and display the same row to all the users? I know i can use the random() method to get the row but how do I display the same row to all the users.
Here's my Controller:-
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Database\Query\Builder::inRandomOrder;
use App\Nothingness;
class NothingnessController extends Controller
{
public function randomQuestion()
{
$seed = Carbon::now()->toISOString();
Nothingness::inRandomOrder($seed)->first();
return view('home', compact('seed'));
}
}
This is a new laravel project
Use Illuminate\Database\Query\Builder::inRandomOrder
method and provide it a seed.
This method is implemented in Eloquent models via __call magic method.
The seed ensures that the results are ordered the same for the same seed.
The following is a query where everyone viewing at the same minute of day sees the same record.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use App\Nothingness;
class NothingnessController extends Controller
{
public function randomQuestion()
{
$seed = Carbon::now()->format('Y-m-d H:i');
$randomNothing = Nothingness::inRandomOrder($seed)->first();
return view('home', compact('randomNothing'));
}
}
You could also have seed come from an external source.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.