【发布时间】:2020-07-14 10:28:46
【问题描述】:
我是 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()”?
请帮帮我
【问题讨论】:
-
您可以在使用 shuffle (array &$array) 从数据库中检索后随机化结果集。 stillat.com/blog/2018/04/22/…
-
在你的 ->get(); 之前尝试一个 inRandomOrder() laravel.com/docs/7.x/queries#ordering-grouping-limit-and-offset
标签: php laravel laravel-5 laravel-7