Aishan's avatar

pagination in laravel 8 goes to the top of the page

I have added pagination method on of the table result in the blade, but I placed the table below the page and every time I click on the next page it takes me to the top of the page which is really annoying scrolling down to the page every time I click on the specific page. Does anybody have a solution to stay right where we first clicked on the page?

{{ $search_result->withQueryString()->links('layouts.paginationlinks') }} 
0 likes
8 replies
mugencoding's avatar

Your pages reload after clicking the next page right? Does your table have an id? I think one of the easiest ways is to concatenate the id of your table at the end of the URL/href of the next page button

url.com?q=samplequery#table_id
Aishan's avatar

@mugencoding Could u please write it down completely? I just cannot appreciate it and the where exactly I should place table ID. Thanks

Aishan's avatar

@MohamedTammam The table is inside blade but query is in the controller, how query function inside controller can recognize table id which is written inside the blade?

mugencoding's avatar

@Aishan you can check the answer of @mohamedtammam on how to concatenate the table id to the URL. For your question

how query function inside controller can recognize table id which is written inside the blade?

if the ID of the table is not dynamic you can create a constant in the controller like

const TABLE_ID = 'myTableWithPagination'

and the rest is in the answer of Mohamed

ayoubhazar20's avatar

if u are using laravel pagination : like bootsrap.blade.php or tailwind try to add this script to those blade :

<script>
document.addEventListener('click', function (event) {
    if (event.target.matches('.page-link[type="button"]')) {
        window.scrollTo({ top: 0, behavior: 'smooth' });
    }
});
</script>

Please or to participate in this conversation.