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

Laravel 5.5 查询分页(含预加载)问题:无法显示分页链接links()

Hey there! Let's figure out why those pagination links aren't showing up—this is a super common gotcha with Laravel pagination, so we'll get it sorted quickly.

The Core Issue: You're Probably Working With a Regular Collection, Not a Paginator Instance

Laravel's links() method only works on pagination objects (specifically LengthAwarePaginator), not on plain Collection instances. If you're using get() to fetch results or converting the query to a regular collection somewhere, that's why the links aren't rendering.

Step-by-Step Fixes

1. Make Sure You're Paginating the Inquiries Query Directly

Since your association chain is Employees ↔ Dealers (many-to-many) → Customers (one-to-many) → Inquiries (one-to-many), you need to target the Inquiries model for pagination, not intermediate models like Customers or Dealers.

For example, if you're fetching inquiries tied to a specific employee, your query should look like this:

// In your controller
use App\Models\Inquiry;

// Fetch inquiries linked to the current employee via the association chain
$thecusts = Inquiry::whereHas('customer.dealer.employees', function ($query) use ($employee) {
    $query->where('employees.id', $employee->id);
})->paginate(20); // This returns a LengthAwarePaginator, not a plain collection

Avoid doing this (it creates a plain collection, which breaks links()):

// ❌ Wrong: get() returns a collection, not a paginator
$thecusts = $employee->dealers()->with('customers.inquiries')->get();

2. Verify You're Calling links() on the Right Object in Your View

In your Blade template, make sure you're calling links() directly on the pagination object you passed:

<!-- Correct: Call links() on the paginator instance -->
{{ $thecusts->links() }}

<!-- ❌ Wrong: If you converted $thecusts to an array/collection, this won't work -->
{{ $thecusts->all()->links() }}

3. Debug to Confirm the Object Type

If you're still stuck, add a quick debug check in your controller to confirm what $thecusts actually is:

dd($thecusts);

Look for Illuminate\Pagination\LengthAwarePaginator in the output. If you see Illuminate\Support\Collection instead, your query isn't using pagination correctly.

4. Handling Nested Association Pagination (If Needed)

If you need to paginate inquiries per customer (instead of all inquiries across customers), you can use lazy eager loading with pagination:

$customers = $employee->dealers()->with(['customers' => function ($query) {
    $query->with(['inquiries' => function ($query) {
        $query->paginate(20);
    }]);
}])->get();

But note that in this case, the pagination links would be rendered per customer's inquiries, not for the entire dataset. Adjust based on your actual use case.

Quick Recap

The key fix is ensuring you're using paginate(20) on the Inquiry query directly (not on a collection of related models) so you get a proper pagination instance. Then calling links() on that instance in your view will render the pagination controls.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 03:37:07