分页HTML表

分页HTML表

问题描述:

在下表中,我把每个学生放在你属于表的老师之下。现在,我想要分页显示在桌下的学生。所以如果第一个选项卡有10个学生,我可以分页到5.分页HTML表

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

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

@DestinatioN,您可以编辑这一点,认为它可能重复?我看到没有重复这个问题:) – Switz

+1

Sry误读你的问题。也许这应该是有帮助的https://laracasts.com/discuss/channels/eloquent/how-to-paginate-relations-in-laravel – DestinatioN

+0

@DestinatioN,请删除重复的标签 – Switz

你确实有一对夫妇语法错误,我想象他们的错字,但以防万一:$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()}} 
+0

在视图中,'@foreach($ students as $ std)'不显示表中的任何内容..我没有在控制器中返回$ students,我确实返回了有效的Json。你知道为什么这可能会发生在视图/ – Switz

+0

你的意思是有效的Json它包含学生?你可以在控制器后的dd($学生)在foreach循环后验证是否有数据 – robbyrr

+0

好的时候,我'dd($ stduents)'或返回$学生后forloop,没有数据显示 – Switz