Laravel单页多表格分页失效及POST路由分页上下文丢失求助
Hey there! Let's tackle your two pagination issues one by one—they're both common gotchas with multi-table pagination and filtered results.
1. Fixing Pagination State Loss Between Tables (GET Route)
You’re spot-on that the custom pagination parameter names (valids, holds, rejects) are the culprit here. When you paginate one table, the URL only includes that table’s page parameter, so switching to another table resets its page to 1.
The simple fix is to pass the current page values of the other tables when generating each pagination link. In your Blade view (administration/ddt-management.blade.php), update each links() call to append the other pagination parameters:
For the valid DDTs table:
{{ $ddtsV->appends(['holds' => request('holds'), 'rejects' => request('rejects')])->links() }}
For the suspended DDTs table:
{{ $ddtsS->appends(['valids' => request('valids'), 'rejects' => request('rejects')])->links() }}
For the rejected DDTs table:
{{ $ddtsR->appends(['valids' => request('valids'), 'holds' => request('holds')])->links() }}
Now when you navigate to page 3 of the suspended table, the URL will keep track of the current page for valid and rejected tables too—switching between tables won’t reset their state anymore.
2. Fixing Lost Company Filter on Pagination (POST Route)
The core problem here is that pagination links default to GET requests, but your filter uses POST. When you click a pagination link, it sends a GET to /ddts instead of your POST route, so the company_id filter gets lost.
The easiest solution is to switch your filter to use GET instead of POST, which plays nicely with pagination. Here’s how to adjust your code:
Step 1: Simplify Your Routes
Replace your two separate routes with a single GET route that handles both filtered and unfiltered cases:
Route::get('/ddts', 'DdtsController@index');
Step 2: Merge Controller Logic
Update your index() method to handle the optional company_id parameter, eliminating the need for a separate indexByCompany method:
public function index(Request $request) { $companyId = $request->get('company_id'); // Base queries for each table $validQuery = Ddt::where('validato', 1); $suspendedQuery = Ddt::where('validato', 0); $rejectedQuery = Ddt::where('validato', 2); // Apply company filter if provided (and not "*") if ($companyId && $companyId !== '*') { $validQuery->where('company_id', $companyId); $suspendedQuery->where('company_id', $companyId); $rejectedQuery->where('company_id', $companyId); } // Paginate with custom parameter names $ddtsValidati = $validQuery->paginate(5, ['*'], 'valids'); $ddtsSospesi = $suspendedQuery->paginate(5, ['*'], 'holds'); $ddtsRifiutati = $rejectedQuery->paginate(5, ['*'], 'rejects'); // Append company_id to all pagination links to preserve the filter $ddtsValidati = $ddtsValidati->appends(['company_id' => $companyId]); $ddtsSospesi = $ddtsSospesi->appends(['company_id' => $companyId]); $ddtsRifiutati = $ddtsRifiutati->appends(['company_id' => $companyId]); $companies = \App\Models\User::where('co2', '!=', 0)->get(); $ddt_urls = Ddt_file_url::all(); return view('administration.ddt-management') ->with('ddtsV', $ddtsValidati) ->with('ddtsS', $ddtsSospesi) ->with('ddtsR', $ddtsRifiutati) ->with('companies', $companies) ->with('ddt_urls', $ddt_urls); }
Step 3: Update Your Filter Form
Change your form method to GET so the company_id is included in the URL (and preserved in pagination links):
<form action="{{ url('/ddts') }}" method="GET"> <select name="company_id"> <option value="*" {{ request('company_id') === '*' ? 'selected' : '' }}>All Companies</option> @foreach($companies as $company) <option value="{{ $company->id }}" {{ request('company_id') == $company->id ? 'selected' : '' }}> {{ $company->name }} </option> @endforeach </select> <button type="submit">Filter</button> </form>
Why This Works:
- The
company_idstays in the URL, so pagination links automatically include it (thanks to theappends()calls in the controller). - You’ll have a single, clean route handling all cases instead of separate GET/POST routes.
- Both the company filter and the page state of all three tables will be preserved across navigation.
内容的提问来源于stack exchange,提问作者JahStation

