You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel搜索分页第2页丢失数据技术问询

Fixing "Missing Data on Page 2" with Laravel's paginate()

Hey there, let's figure out why your pagination is dropping data when you jump to page 2—this is a super common gotcha with Laravel search workflows, so I’ll walk you through the most likely fixes:

This is the #1 culprit. When you submit your search via POST, Laravel’s pagination generates GET links by default. Those links won’t automatically carry over your search terms (like omschrijving, artikelnummer, etc.), so when you click page 2, the query runs without your original filters—hence no data.

Fix this by using the appends() method to attach all your search parameters to the pagination links:

$products = Product7Days::where('omschrijving', 'LIKE', '%'.$request->omschrijving.'%')
    ->where('artikelnummer', 'LIKE', '%'.$request->artikelnummer.'%')
    ->whereIn('relatienummer', $leverancierenArray)
    ->whereIn('omzetgroep', $omzetgroepenArray)
    ->whereIn('btwcode', $BTWArray)
    ->whereBetween('prijs', [$prijsVan, $prijsTot])
    ->where('totaalaantal', '>=', $aantalVan)
    ->paginate(50)
    // Append only the search params you need (avoids carrying sensitive data)
    ->appends($request->only(['omschrijving', 'artikelnummer', 'prijsVan', 'prijsTot', 'aantalVan']));

If you don’t have sensitive data in your request, you can simplify it to:

->appends($request->all());

Now when you render pagination links in your view, Laravel will add all your search parameters to the URL (e.g., /your-route?page=2&omschrijving=foo&artikelnummer=bar), so the second page query uses the same filters as the first.

2. Make Sure You’re Paginating the Query Builder (Not a Collection)

Double-check that you aren’t calling get(), first(), or converting the query to a collection before paginate(). If you do that, you’re paginating an in-memory collection instead of letting the database handle pagination—and if the collection is smaller than 50 items, page 2 will be empty. Your current code looks like it’s using the query builder directly, but it’s worth confirming!

3. Verify Your whereIn Arrays Are Consistent

Even though page 1 has data, make sure arrays like $leverancierenArray, $omzetgroepenArray, and $BTWArray are being re-generated for every request (including the GET request for page 2). If these arrays are only created during the initial POST search, they’ll be empty on page 2, leading to an empty result set.

For example, if you’re generating these arrays from request parameters, make sure you’re pulling those values from the request (not just temporary variables) so they exist for both POST and GET requests.

4. Check for Pagination Parameter Conflicts

Rarely, the default page parameter might be overwritten by a route, middleware, or other code. To test this, dump $request->page in your controller when loading page 2—if it’s not 2, that’s the issue.

You can specify a custom pagination parameter to avoid conflicts:

->paginate(50, ['*'], 'product_page');

This will use product_page=2 instead of page=2 in your pagination links.

Quick Debug Test

Manually type the page 2 URL into your browser with all your search parameters (e.g., /your-route?page=2&omschrijving=your-term&artikelnummer=your-number). If this returns data, you know the problem is missing parameters in your pagination links (fix #1). If it still returns nothing, double-check your whereIn arrays or query logic.

内容的提问来源于stack exchange,提问作者MrZzippo p

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:37:50