A
A
Andrey Shubnikov2020-02-21 18:26:17
Laravel
Andrey Shubnikov, 2020-02-21 18:26:17

How to optimize the display of all elements on the page?

Hello! How can I optimize the output of all elements on the page? Let's say there are more than 10,000 of them, then the page loads more than 2-3 seconds.

$cat = Category::with('translations.team')->where('slug', '=', $slug)->firstOrFail();
            $translation = $cat->translations;
            $translations = CollectionHelper::paginate($translation, $translation->count(), 15);
            
            return view('translation.index', compact('cat'))->with([
                'category' => $cat,
                'translations' => $translations,
            ]);

Answer the question

In order to leave comments, you need to log in

3 answer(s)
J
jazzus, 2020-02-22
@Andrew_Shbov

$category = Category::whereSlug($slug)
                    ->firstOrFail();
                    
$translations = $category->translations()
                         ->with('team')
                         ->paginate(15);
                         
return view('translation.index', compact('category', 'translations'));

Y
Yuri Kulaxyz, 2020-02-21
@Kulaxyz

Version 6 added Lazy Collections .

A
Alexander Aksentiev, 2020-02-22
@Sanasol

Why are you fetching all the data if you then paginate and display only 15 records?
Why not get only these 15 records from the database?
And why not do it not through with (), but with a regular request to the desired model, and then it already checks that there is a category with the desired slug.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question