HTML表格分页

时间:2017-12-18 08:42:09

标签: php laravel

在下表中,我把每个学生都放在你所属的老师手中。现在,我想对在桌子下面显示的学生进行分页。因此,如果第一个标签有10个学生,我可以分页到5.

目前,我的代码并没有做我想要实现的目标。我如何实现这一目标?

PS:我知道如果我想对teachers->links()等教师进行分页,语法会是怎样的

控制器

$teacher = Teacher::where('branch_id', Auth::user()->branch_id);
return view('create',compact('teachers));

HTML

<div class="nav-tabs-custom" id="tabs">
    <ul  id="myUL" class="nav nav-tabs">
        @foreach($teachers as $teacher)
            <li ><a href="#tab_{{ $teacher->id }}" data-toggle="tab" >{!!$teacher->name!!}</a></li>
        @endforeach
    </ul>
    <div class="tab-content">
        @foreach($teachers as $key => teacher)
            <div class="tab-pane" id="tab_{{ $teacher->id }}">
                <table class="table" id="tables">
                    <thead>
                        <tr>
                            <th></th>
                            <th colspan="5"></th>
                            <th></th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach($teacher->students as $std)
                            <tr>
                                <td></td>
                                <td>{{$std->name }}</td>
                                <td></td>
                            </tr>
                        @endforeach       
                    </tbody>
                </table>
                {{$teacher->stduents->links()}}
            </div>
        @endforeach
    </div>
</div>

1 个答案:

答案 0 :(得分:0)

你确实有一些语法错误,我想象他们的拼写错误,但以防万一:控制器中的$teacher应为复数,@foreach($teachers as $key => teacher)值应为$teacher{{$teacher->stduents->links()}}应该是学生

在您的控制器中,您可以这样做:

$teachers = Teacher::where('branch_id', Auth::user()->branch_id)->get();

foreach ($teachers as $teacher) {
    $students = $teacher->students()->paginate(5);
}

return view('index', compact('teachers', 'students'));

在你看来:

        @foreach($students as $std)
            <tr>
               <td></td>
               <td>{{$std->name }}</td>
               <td></td>
            </tr>
        @endforeach
    </tbody>
</table>
{{$students->links()}}

这也应该有用。在教师模型中:

public function paginateStd()
{
    return $this->students()->paginate(5);
}

在你看来:

    @foreach($teacher->paginateStd() as $std)
       <tr>
           <td></td>
           <td>{{$std->name }}</td>
           <td></td>
       </tr>
    @endforeach
    </tbody>
</table>
{{$teacher->paginateStd()->links()}}