试图使用laravel分页打破我的控制器/视图
问题描述:
我试图让分页工作在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
答
我还没有深入阅读这个主题。它现在正在工作,这是我必须改变的视图:
//from:
@foreach($locations as $location)
//to:
@foreach($locations->results as $location)
这只返回5个结果。然后将链接添加到我添加了这个HTML /刀片我的表的页脚:
<tfoot>
<tr>
<td colspan="5"> {{ $locations->links() }} </td>
</tr>
</tfoot>
希望别人好处,我认为这没有在文档非常清晰。