Answer the question
In order to leave comments, you need to log in
How to make products static and not random?
Hello, there is a script that randomly displays products from the database!
With a large number of records from 300k, the site starts to slow down wildly!
The task is that the goods are not taken randomly, but are static!
there are 3 files
first
../public_html/laravel/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php
there is code
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);
}
Answer the question
In order to leave comments, you need to log in
Replace calls to inRandomOrder()
with the desired static filter, of course.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question