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

Laravel分页点击页码后数据无变化,求助排查问题

Laravel分页点击页码后数据无变化问题排查

问题描述

在Laravel项目中添加分页功能,按教程操作后点击新页码页面数据未发生变化,具体操作如下:

  • Controller中添加代码:$competitions = Competition::latest()->paginate(1);
  • 在app/Providers/AppServiceProvider引入use Illuminate\Pagination\Paginator;,并在boot方法中添加Paginator::useBootstrap();
  • Blade视图表格闭合后添加{{ $competitions->onEachSide(1)->links() }}

补充代码信息

Controller的index方法:

public function index()
{
    $competitions = Competition::latest()->paginate(1);

    return view('competition.index', compact('competitions')); 
}

Blade循环代码:

@foreach ($competitions as $competition)      
<tbody >
   @switch($competition->status)
        @case('active')
            <tr>
        @break

        @case('to_push')
            <tr class="bg-secondary">
        @break

        @case('inactive')
            <tr class="bg-warning">
        @break

        @default
            <tr>
        @endswitch
        <td>{{ $competition->id }}</td>
        <td>{{ $competition->name }}</td>
        <td>{{ $competition->status }}</td>
        <td>{{ $competition->mode }}</td>
        <td>{{ $competition->type }}</td>
        <td>{{ $competition->frequency }}</td>
        <td>{{ $competition->last_push }}</td>
        <td>{{ $competition->next_push }}</td>
        <td class="text-center"><a class="btn btn-info btn-sm" href="{{ route('events.list_by_competition',['competition' => $competition->id]) }}">Events</a></td>   
        <td class="text-center"><a class="btn btn-info btn-sm" href="{{ route('competitions.exports',$competition->id) }}">Exports</a></td>
        <td class="text-center">
            <form action="{{ route('competitions.destroy',$competition->id) }}" method="POST">

                <a class="btn btn-primary btn-sm" href="{{ route('competitions.edit',$competition->id) }}">Edit</a>

                @csrf
                @method('DELETE')
  
                <button type="submit" class="btn btn-danger btn-sm">Delete</button>
            </form>
        </td>

    </tr>
</tbody>
@endforeach
</table>
{{ $competitions->links('pagination::bootstrap-5') }}

dd(Competition::all())返回4条有效记录,请求排查问题原因。


排查与解决方法

1. 修复Blade视图结构错误

你的@foreach循环将<tbody>标签包裹在内部,会导致每一条数据生成独立的<tbody>节点,浏览器渲染时仅会显示第一个<tbody>的内容,后续页码的数据即使加载成功也无法正常展示。

修正代码:将<tbody>移至@foreach循环外部:

<tbody>
@foreach ($competitions as $competition)      
   @switch($competition->status)
        @case('active')
            <tr>
        @break

        @case('to_push')
            <tr class="bg-secondary">
        @break

        @case('inactive')
            <tr class="bg-warning">
        @break

        @default
            <tr>
        @endswitch
        <!-- 表格内容保持不变 -->
    </tr>
@endforeach
</tbody>
</table>
{{ $competitions->links('pagination::bootstrap-5') }}

2. 验证分页参数传递

点击页码后检查浏览器地址栏是否带有?page=2这类参数:

  • 若参数不存在,确认路由配置为GET请求(默认资源路由的index方法为GET,无需额外修改);
  • 若参数存在,在Controller的index方法中添加dd(request('page')),确认是否能正确获取页码参数,验证分页逻辑是否正常接收参数。

3. 清除Laravel缓存

执行以下命令清除缓存,避免配置或视图缓存导致的异常:

php artisan cache:clear
php artisan view:clear
php artisan config:clear

4. 验证分页实例正确性

在Controller中输出分页实例信息,确认分页数据是否正确:

public function index()
{
    $competitions = Competition::latest()->paginate(1);
    // 查看分页数据、当前页码等信息
    dd($competitions);
    return view('competition.index', compact('competitions')); 
}

检查返回结果中的data数组是否对应当前页码的记录,current_page是否与请求页码一致。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 21:01:11