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

HoofBeats's avatar

Getting data Reset API

Hello everyone, how can I get 200,000 API records and not break the server :) Thanks

{
    $items = [];

    $contractNumber = $request->get('limit');

    /** @var User[] $rows */
    $rows = User::query()
        ->orderByDesc('created_at')
        ->limit($contractNumber)
        ->get();

    foreach ($rows as $row) {
        $items[] = $row->toArray();
    }

    return response()->json(['items' => $row]);
}
0 likes
4 replies
SilenceBringer's avatar

@hoofbeats it's not good way to returns so many records. Because if you'll have a lot of requests to returns 200,000 records at the same time - you server will die anyway. Force to use pagination

HoofBeats's avatar

I need to make sure that people receive a lot of entries in parts.

How can i do this? let's say 5000 records in 2 seconds and until we get to the end

SilenceBringer's avatar
Level 55

@hoofbeats not sure I understand you.

Add page param to your request and use it to get the next pages

for example, for limit = 5000 and page = 2/3/4/...

    $contractNumber = $request->get('limit');

    $offset = ($request->get('page', 1) - 1) * $contractNumber

    /** @var User[] $rows */
    $rows = User::query()
        ->orderByDesc('created_at')
        ->limit($contractNumber)
        ->offset($offset)
        ->get();
automica's avatar

You can easily achieve this out of the box by creating an API Resource for your User model and then using the paginator on a collection

https://laravel.com/docs/8.x/eloquent-resources#pagination

eg

use App\Http\Resources\UserCollection;
use App\Models\User;

Route::get('/users', function () {
    return new UserCollection(User::paginate());
});

This will give you an object such as:

{
    "data": [
        {
            "id": 1,
            "name": "Eladio Schroeder Sr.",
            "email": "[email protected]",
        },
        {
            "id": 2,
            "name": "Liliana Mayert",
            "email": "[email protected]",
        }
    ],
    "links":{
        "first": "http://example.com/pagination?page=1",
        "last": "http://example.com/pagination?page=1",
        "prev": null,
        "next": null
    },
    "meta":{
        "current_page": 1,
        "from": 1,
        "last_page": 1,
        "path": "http://example.com/pagination",
        "per_page": 15,
        "to": 10,
        "total": 10
    }
}

and then you can pass in paginator parameters as you would normally do

eg

/users?page=2

Please or to participate in this conversation.