Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Dárcio Júnior's avatar

Method paginate - Laravel

Good morning guys, I have a complex query that takes about 3 seconds to execute returning 113 thousand records with about 10 joins, etc, etc ...

So far ok, but because of paging it is as if we generated 2 queries one to know the total of records and the other to bring the records that would take 6 seconds ... After all the pagination does a count with the * of all tables which would be totally unnecessary because I use the paginate method ...

Would it be possible to make a custom pagination for this case? At first with some laravel feature even without using the traditional paginate ???

The intention would be to change the query that generates the count with * to count(0) because the * represents that I want to count all fields from all tables and would give this catastrophe, where if I pass a single field ready already solves, it drops from 3 seconds to 1 second ...

Does anyone have any suggestions ??? Thanks for now ...

0 likes
12 replies
jlrdw's avatar

Use a lengthaware, and first pass store count in session. Then page 2, 3, etc you don't need to count again.

1 like
MohamedTammam's avatar
Level 51

@Dárcio Júnior If that solves your problem, please mark his answer as "Best answer" to close the discussion.

Dárcio Júnior's avatar

@jlrdw good afternoon friend ...

I'm having a single problem only, the paging is failing to retrieve the filtered parameters, follows my code below, where am I going wrong ????

public function paginate($items, $perPage = 5, $page = null) { $items = $items instanceof Collection ? $items : Collection::make($items); $page = $page ?: (Paginator::resolveCurrentPage() ?: 1);

    $options = [
        'path' => request()->url(),
        'query' => request()->query()
    ];

    return new LengthAwarePaginator($items->forPage($page, $perPage)->values(), $items->count(), $perPage, $page, $options);
}
Dárcio Júnior's avatar

@jlrdw I am working withQueryString in the pagination view -> {{ $tableData->withQueryString()->links() }}

jlrdw's avatar

@Dárcio Júnior just pass the parameters as an array to appends. I have had no problems with the paginator doing this.

@php echo str_replace('/?', '?', $report->appends($array-of-parameters)->render(')  @endphp

If you named it $myparameters, then:

@php echo str_replace('/?', '?', $report->appends($myparameters)->render())  @endphp
1 like
Dárcio Júnior's avatar

@jlrdw I understood ... Perfectly in the view ... In the controller, I made this way:

public function paginate($items, $perPage = 5, $page = null) { $items = $items instanceof Collection ? $items : Collection::make($items); $page = $page ?: (Paginator::resolveCurrentPage() ?: 1);

    $params = ['query' => request()->query(), 'page' => $page];

    $options = [
        'path' => request()->url(),
        'params', $params
    ];

    return new LengthAwarePaginator($items->forPage($page, $perPage)->values(), $items->count(), $perPage, $page, $options);
}
Dárcio Júnior's avatar

@jlrdw Good afternoon friend, just this code here already solved, it was a problem in an object of my form with respect to the parameters ...

public function paginateLengthAwarePaginator($items, $perPage = 5, $page = null, $options = []) { $items = $items instanceof Collection ? $items : Collection::make($items);

    $page = $page ?: (Paginator::resolveCurrentPage() ?: 1);

    $options = [
        'path' => request()->url(),
        'query' => request()->query()
    ];

    return new LengthAwarePaginator($items->forPage($page, $perPage), $items->count(), $perPage, $page, $options);
}

Your answer was the best ... Thanks

Translated with www.DeepL.com/Translator (free version)

Please or to participate in this conversation.