我遇到了问题。这是我第一次使用分页。
这是我的观点:
@extends('base')
@section('title', 'News')
@stop
@section('main')
@if ($news->count())
@foreach($news as $news)
<div class="container">
<div class="doc-content-box">
<legend>{{ $news->title }} <div id="spaceholder"></div>
<p>
<h6><span class="muted"><i class="icon-calendar"></i> {{ $news->created_at }} <a href="/profile/{{ $news->author }}">{{ $news->author }}</a><span>
</p>
</h6>
</legend>
<div style="margin-top:7px">
<p>{{ $news->content }}</p>
</div>
<div class="btn-group">
<a class="btn btn-primary" href="{{ URL::route('news.edit', $news->id) }}"><i class="icon-pencil"></i> Edit</a>
<a class="btn btn-danger" href="{{ URL::route('news.destroy', $news->id) }}"><i class="icon-trash icon-large"></i> Delete</a>
</div>
</div> <!-- /container -->
<div id="spaceholder"> </div>
@endforeach
{{ $news->links() }}
@else
<div class="container">
<div class="doc-content-box">
<legend>News</legend>
<p>There are no news.<br /><br />If you wish to create a news, you should go to our <a href="{{ URL::to('news') }}">news management page!</a></p>
</div>
</div>
@endif
@stop
然后导致错误Call to undefined method Illuminate\Database\Query\Builder::links()
。
我也尝试将@foreach($news as $news)
更改为@foreach($news->results as $news)
,但后来又出现了新的错误:
Undefined property: Illuminate\Pagination\Paginator::$results
分页器本身有效,但分页链接(下一个和上一个)不起作用。
答案 0 :(得分:1)
当您的查询尚未执行时会发生这种情况。您的变量$news
是查询构建器,而不是对象数组。在致电之前:
$news->links();
您需要通过调用:
来执行查询$news = $news->get();
然后在调用get()
方法之后,它将返回一个表示结果集的数组,而不是Illuminate\Database\Query\Builder
的实例。
注意:当您在Illuminate\Database\Query\Builder
的实例中执行foreach时,它将执行查询,但结果将在foreach循环中作用域。这就是你无法检索分页链接的原因,因为你引用的对象是查询而不是结果。
答案 1 :(得分:0)
如果您测试该代码:
@foreach($news as $new)
<div class="container">
<div class="doc-content-box">
<legend>{{ $new->title }} <div id="spaceholder"></div>
<p>
<h6><span class="muted"><i class="icon-calendar"></i> {{ $new->created_at }} <a href="/profile/{{ $new->author }}">{{ $new->author }}</a><span>
</p>
</h6>
</legend>
<div style="margin-top:7px">
<p>{{ $new->content }}</p>
</div>
<div class="btn-group">
<a class="btn btn-primary" href="{{ URL::route('news.edit', $new->id) }}"><i class="icon-pencil"></i> Edit</a>
<a class="btn btn-danger" href="{{ URL::route('news.destroy', $new->id) }}"><i class="icon-trash icon-large"></i> Delete</a>
</div>
</div> <!-- /container -->
<div id="spaceholder"> </div>
@endforeach