2017-12-18 39 views
1

在下表中,我把每個學生放在你屬於表的老師之下。現在,我想要分頁顯示在桌下的學生。所以如果第一個選項卡有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

回答

1

你確實有一對夫婦語法錯誤,我想象他們的錯字,但以防萬一:$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