laravel 相当于随机排序

我是 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()”?


请帮我


翻翻过去那场雪
浏览 106回答 1
1回答

郎朗坤

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();
打开App,查看更多内容
随时随地看视频慕课网APP