samedi 27 juin 2020

How to replace request method with static? inRandomOrder

random requests greatly slow down the site, products change on the main site, with each update! I want to make it static! Here is the code! help me please!

public function index()
    {
      //homepage
      $settings =Setting::first();
      $credit = Credit::first();
      $rate_per_click = $credit->rate_per_click;
      //random products
      $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);
    }

This code is on the product page!

//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);
    }

Sorry for my bad english!



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2ZdJyAW
via IFTTT

Aucun commentaire:

Enregistrer un commentaire