- 要求每次打开看到的都是不同的热卖商品
- 商品依旧是热卖,是随机的
- 随机时不能出现两个一模一样的商品
/**
* 热卖商品
* 刷新随机展示四条
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public static function hot(){
$data = \app\home\model\Goods::where('is_hot',1)->orderRaw('rand()')->limit(4)->select()->toArray();
return $data;
}
这段代码展示了如何从数据库中随机选取不重复的热卖商品。通过`where`条件筛选出热卖商品,利用`orderRaw('rand()')`进行随机排序,再使用`limit(4)`限制返回数量,确保每次展示的商品都是不同的。
232

被折叠的 条评论
为什么被折叠?



