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

Spiral's avatar

custom pagination with limit & offset

Hi!

I have 100 records in the database. I want to show per page 10 if the user click on the show more then I will give him 10 more records with the existing 10 records with limit & offset

plz give me a solution so or link wherefrom I could solve my problem

0 likes
14 replies
Spiral's avatar

@jtemple042996 Thanks brother for replying...

======

I want not to use model pagination I want to use custom pagination in laravel because I have 1000 records in the database I want to show only 10 records limit if user click for more show then I want show more 10 records then will show 20

tykus's avatar

I believe the OP is wanting to implement Infinite Scrolling @chaudigv? Horrible UX in my opinion; sometimes the user does not want to load more...

1 like
Spiral's avatar

@chaudigv Brother, I want to make custom pagination and send in JSON response so that user will have only 10 records if click show more with model id then I send more 10 from the database

Spiral's avatar

@tykus Yes Bro.. You are right i want to like this pagination.. Can you give me a solution or snippet so that solves my problem

Spiral's avatar

@tykus bro.. I'm making this on the API side and want to send it to the react developer from the backend if he will click the show more then I will give more than 10 from the database

tykus's avatar

What is React giving the backend; it would seem the minimum you would get is a page query parameter?

1 like
Spiral's avatar

React developer want 10 records from me on the first time whereas I have 100 of records in the database but want to send 10 records first time after if he clicks show more 10 records then I will give 10 records with existing 10 from the database.

we did not want to send more data from the backend when the frontend did not want to if he wants then will send 10 more whatever

Brother, maybe now you can find my logic that I want

Spiral's avatar

Yes, @michaloravec dear.. You are right if the developer want to get more 10 records for clicking the show more then I will give him more 10 with offset 10 records

Spiral's avatar

Can you give me an example or link where I will get the concept and put the query in project

tykus's avatar

The React application literally needs only to send a page query parameter on the request to your backend; this will automatically be handled by the Builder's paginator. All you need is to paginate the query:

$results = Model::where(/* constraints */)->orderBy(/* sort direction */)
	->paginate(10);

return response()->json($results);

The JSON structure of the Paginator instance will contain all of the necessary data, and the meta information described above:

{
   "total": 50,
   "per_page": 10,
   "current_page": 1,
   "last_page": 4,
   "first_page_url": "http://laravel.app?page=1",
   "last_page_url": "http://laravel.app?page=5",
   "next_page_url": "http://laravel.app?page=2",
   "prev_page_url": null,
   "path": "http://your-domain.app",
   "from": 1,
   "to": 10,
   "data":[
        {
            // Record...
        },
        {
            // Record...
        }
   ]
}

Once the React developer understands this structure they can extract the data to be rendered, and interrogate the meta data to understand if there are more records, or not.

1 like

Please or to participate in this conversation.