[英]How do I fetch a radom row from the database and display the same row to all the users in laravel?
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.
我知道我可以使用 random() 方法来获取行,但是如何向所有用户显示同一行。
Here's my Controller:-这是我的 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这是一个新的 laravel 项目
Use Illuminate\Database\Query\Builder::inRandomOrder
method and provide it a seed.使用
Illuminate\Database\Query\Builder::inRandomOrder
方法并为其提供种子。
This method is implemented in Eloquent models via __call magic method.
此方法在 Eloquent 模型中通过 __call 魔术方法实现。
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.您也可以让种子来自外部来源。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.