尝试使用laravel分页会破坏我的控制器/视图

时间:2013-04-04 14:59:40

标签: pagination laravel eloquent laravel-3

我正试图让分页在Laravel工作。我之前没有使用分页,所以我试图使用文档和谷歌搜索结果中的一些例子。我想知道我是否需要在config中的某个地方打开'分页'。

这是我简单的工作控制器:

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

1 个答案:

答案 0 :(得分:0)

我没有深入阅读这个主题。它现在正在工作,这是我必须在视图中改变的地方:

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

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

仅返回了5个结果。然后,为了添加链接到我的表格的页脚,我添加了这个HTML / Blade:

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

希望别人受益,因为我没有在文档中发现这一点非常清楚。