亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 148 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號