2013-04-04 24 views
0

我試圖讓分頁工作在Laravel。我之前沒有使用過分頁,因此我試圖使用文檔和Google結果中的一些示例。我想知道是否需要在配置中的某個地方打開「分頁」。試圖使用laravel分頁打破我的控制器/視圖

這裏是我的簡單的工作控制器:

public function get_index() { 
    $locations = Location::all(); 
    return View::make('location.index')->with('locations', $locations)->render(); 
} 

當我嘗試添加分頁這樣它打破:

public function get_index() { 
    $locations = Location::paginate(5); 
    return View::make('location.index')->with('locations', $locations)->render(); 
} 

..和我得到的錯誤:

Unhandled Exception 

Message: 
Error rendering view: [location.index] 

Trying to get property of non-object 

這裏是我的location.index視圖:

@layout('layouts.blank') 
@section('content') 
<div class="row"> 
<div class="twelve columns"> 
    <div role="main" id="content"> 
     <h3>Locations - All</h3> 
     @if($locations) 
      <table class="twelve"> 
       <thead> 
        <tr> 
         <th style="text-align: left;">Address</th> 
         <th>Area</th> 
         <th>Region</th> 
         <th>Edit</th> 
         <th>Delete</th> 
        </tr> 
       </thead> 
       @foreach($locations as $location) 
       <tbody> 
        <tr> 
         <td style="text-align: left;">{{ $location->company,', ',$location->add_name,', ',$location->add_city }}</td> 
         <td> – </td> 
         <td> – </td> 
         <td>{{ HTML::link('location/update/'.$location->id, '',array('class'=>"foundicon-edit updateicon")) }}</td> 
         <td>{{ HTML::link('location/delete/'.$location->id, '',array('class'=>"foundicon-remove warnicon")) }}</td> 
        </tr> 
       </tbody> 
       @endforeach 
      </table> 
     @else 
      Looks like we haven't added any locations, yet! 
     @endif 
     <p>{{ HTML::link('location/create', 'Create a location', array('class'=>"small radius button")) }}</p> 
    </div> 
</div> 
</div> 
@endsection 

回答

0

我還沒有深入閱讀這個主題。它現在正在工作,這是我必須改變的視圖:

//from: 
@foreach($locations as $location) 

//to: 
@foreach($locations->results as $location) 

這隻返回5個結果。然後將鏈接添加到我添加了這個HTML /刀片我的表的頁腳:

<tfoot> 
    <tr> 
    <td colspan="5"> {{ $locations->links() }} </td> 
    </tr> 
</tfoot> 

希望別人好處,我認爲這沒有在文檔非常清晰。

相關問題