为了账号安全,请及时绑定邮箱和手机立即绑定

laravel 相当于随机排序

laravel 相当于随机排序

PHP
翻翻过去那场雪 2023-08-06 15:30:57
我是 Laravel 的初学者。我在 Laravel 7 中有一个项目。我有这个代码:public function getPromoProducts()    {        return $this->model->select('name', 'slug', 'products.id', 'small_description', 'promo_desc')->with(['features', 'frontImage'])->active()->leftJoin('selected_product_features', function ($join) {            $join->on('products.id', '=', 'selected_product_features.product_id');        })->where('selected_product_features.key', 'price_promo')->where('selected_product_features.description', '<>', 0)->limit(2)->get();    }我怎样才能从传统的mysql添加到这个代码“ORDER BY RAND()”?请帮我
查看完整描述

1 回答

?
郎朗坤

TA贡献1921条经验 获得超9个赞

Laravel 有inRandomOrder()方法,在查询生成器上调用它。在引擎盖下,它将使用以下内容进行订购。

return $this->model->select('name', 'slug', 'products.id', 'small_description', 'promo_desc')
    ->with(['features', 'frontImage'])
    ->active()
    ->leftJoin('selected_product_features', function ($join) { 
           $join->on('products.id', '=', 'selected_product_features.product_id');
    })->where('selected_product_features.key', 'price_promo')
    ->where('selected_product_features.description', '<>', 0)
    ->limit(2)
    ->inRandomOrder()
    ->get();


查看完整回答
反对 回复 2023-08-06
  • 1 回答
  • 0 关注
  • 60 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信