winsom
@winsom
так себе

Как сделать статичным а не рандомным товары?

Привет есть скриптец который рандомно выводит товары из бд!
При большом количестве записей от 300к, товаров сайт начинает дико тормазить!
Задача чтобы товары не брались рандомно а были статичными!
есть 3 файла
первый
../public_html/laravel/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php
есть код
public function inRandomOrder($seed = '')
    {
        return $this->orderByRaw($this->grammar->compileRandom($seed));
    }

потом в контралере фронтенде
public function index()
    {
      $settings =Setting::first();
      $credit = Credit::first();
      $rate_per_click = $credit->rate_per_click;

      $products = Product::whereHas('user', function($q)use ($rate_per_click) {
        $q->where('credit', '>',$rate_per_click);
        $q->where('active', 1);
        })->inRandomOrder()->limit($settings->home_rand_pro)->get();

         //get 4 latest posts
      $posts = Post::orderBy('id', 'desc')->take($settings->home_posts)->get();
      $users = User::where('active', '=',1)->take($settings->home_users)->get();

      return view('index')
->with('latest_product',Product::whereHas('user', function($q)use ($rate_per_click) {
        $q->where('credit', '>',$rate_per_click);
        $q->where('active', 1);
        })->orderBy('id','desc')->first())
->with('most_viewed_product',Product::whereHas('user', function($q)use ($rate_per_click) {
        $q->where('credit', '>',$rate_per_click);
        $q->where('active', 1);
        })->orderBy('views_count','desc')->first())
->with('most_clicked_product',Product::whereHas('user', function($q)use ($rate_per_click) {
        $q->where('credit', '>',$rate_per_click);
        $q->where('active', 1);
        })->orderBy('click_count','desc')->first())
    ->with('categories',(Category::all()))
    ->with('pages',(Page::all()))
    ->with('slides',(Slider::all()))
    ->with('products',$products)
    ->with('posts',$posts)
    ->with('users',$users)
    ->with('settings',$settings);
    }

и в контролере продукта
//get random Products
      $rand_products = Product::whereHas('user', function($q)use ($rate_per_click) {
        $q->where('credit', '>',$rate_per_click);
        $q->where('active', 1);
        })->inRandomOrder()->limit(5)->get();

      return view('product_page')
      ->with('product',$product)
      ->with('compared_products',$compared_products)
      ->with('rand_products',$rand_products)
      ->with('categories',(Category::all()))
      ->with('pages',(Page::all()))
     ->with('settings',$settings);
    }

Что надо сделать чтобы рандомно не брались запросы в бд, а были статичными!
  • Вопрос задан
  • 119 просмотров
Решения вопроса 1
alexey-m-ukolov
@alexey-m-ukolov Куратор тега Laravel
Заменить обращения к inRandomOrder() на нужный статичный фильтр, разумеется.
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы