我是 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();- 1 回答
- 0 關注
- 148 瀏覽
添加回答
舉報
0/150
提交
取消
