laravel 等价随机排序
laravel equivalent order by random
我是 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()”?
请帮帮我
Laravel 有 inRandomOrder()
方法,在查询生成器上调用它。在引擎盖下它将使用 following 进行排序。
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();
我是 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()”?
请帮帮我
Laravel 有 inRandomOrder()
方法,在查询生成器上调用它。在引擎盖下它将使用 following 进行排序。
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();