laravel paginator确实有效,但在我的情况下并不好。
所以我有一个包含评论的论坛,当该页面上有超过10条评论时,必须显示分页符。
所以我有一个主题超过10条评论然后它在该页面上正常工作,但是当我转到另一页时,他确实采用了大多数评论的分页。那不是我想要的。
以下是一些屏幕和代码:
这里有超过10条评论,所以显示分页符很好。这里奇怪的是它只显示 2 条评论,而不是10条。
这是另一个只有8条评论的主题,所以分页不应该显示任何内容:
但正如你所看到的,它确实显示了分页器......
我的代码如下:
控制器:
public function thread($title)
{
$thread = Thread::where('title', '=', str_replace('Thread-', '',str_replace('-', ' ', $title)))->first();
$usercolor = DB::table('threads')
->join('user', 'threads.uid', '=', 'user.id')
->join('role', 'user.role_id', '=', 'role.id')
->where('threads.uid', '=', $thread->uid)
->first();
$usertitle = DB::table('threads')
->join('user', 'threads.uid', '=', 'user.id')
->where('threads.uid', '=', $thread->uid)
->first();
$comments = Comment::orderBy('posted_at', 'asc')
->where('deleted_at', '=', '0000-00-00 00:00:00')
->with('user.role')
->paginate(10);
if (!$thread) {
return die('bestaat niet!');
}else{
return View::make('thread')->with('threads', $thread)
->with('comments', $comments)
->with('usercolor', $usercolor)
->with('usertitle', $usertitle);
}
}
查看:
@include('globs.header')
<div class="col-sm-offset-1 col-sm-10">
<div class="clearfix"> </div>
@if(Request::get('page', 1) == 1)
<div class="panel panel-default" id="a9">
<div class="panel-heading">
<div class="panel-title">{{ $threads->title }}
</div>
</div>
<div class="panel-body thread-row">
<div class="row thread-row">
<div class="col-md-2 col-sm-3 text-center userblock">
<div class="clerafix"> </div>
<img class="img-thumbnail avatar" src="./public/img/avatar.jpg" alt="Avatar" height="55px;" width="55px">
<div class="push_bottom_5" style="color:{{ $usercolor->colour }};"><strong>{{ $threads->username }}</strong></div>
<div class="push_bottom_5"><small>{{ $usertitle->usertitle }}</small></div>
<div class="label label-default" style="background-color:{{ $usercolor->colour }};">{{ $usercolor->name }}</div>
<div class="push_bottom_5">
@if($usercolor->id == '1')
<i class="fa fa-star"></i>
@elseif($usercolor->id == '2')
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
@elseif($usercolor->id == '3')
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
@elseif($usercolor->id == '4')
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
@elseif($usercolor->id == '5')
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
@elseif($usercolor->id >= '6')
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
@endif
</div>
<div class="text-muted text-left"><small>Total Posts: 66</small></div>
<div class="text-muted text-left"><small>Joined 05-11-2014</small></div>
<div class="clerafix"> </div>
</div>
<div class="col-md-5 col-sm-6 col-xs-8">
<div class="clerafix"> </div>
<div class="text-muted"><span><i class="fa fa-calendar"></i> </span><small>
<span>Posted:</span> {{ date("d-m-Y", strtotime($threads->date_posted)) }}</small></div>
</div>
<div class="col-md-10 col-sm-9 col-xs-12">
<div class="clerafix"> </div>
<div class="content_body"><p>{{ Helper::Filter(BBCode::parse($threads->message)) }}</p>
</div>
<div class="clerafix"> </div>
</div>
</div>
</div>
<div class="panel-footer clearfix">
<div class="pull-right">
<!-- acties -->
<button class="btn btn-primary btn-sm"><i class="fa fa-edit"></i> Aanpassen</button>
<button class="btn btn-primary btn-sm"><i class="fa fa-quote-left"></i> Quote</button>
<button class="btn btn-danger btn-sm"><i class="fa fa-trash"></i> Verwijder</button>
<button class="btn btn-warning btn-sm"><i class="fa fa-exclamation"></i> Rapporteer</button>
</div>
</div>
</div>
<!-- end of the topic -->
</div>
@endif
<!-- begin of comments -->
@foreach($comments as $comment)
@if($comment->tid == $threads->tid)
<div class="col-sm-offset-1 col-sm-10">
<div class="clearfix"> </div>
<div class="panel panel-default" id="a9">
<div class="panel-body thread-row">
<div class="row thread-row">
<div class="col-md-2 col-sm-3 text-center userblock">
<div class="clerafix"> </div>
<img class="img-thumbnail avatar" src="./public/img/avatar.jpg" alt="Avatar" height="55px;" width="55px">
<div class="push_bottom_5" style="color:{{ $comment->user->role->colour }};"><strong>{{ $comment->username }}</strong></div>
<div class="push_bottom_5"><small>{{ $comment->user->usertitle }}</small></div>
<div class="label label-default" style="background-color:{{ $comment->user->role->colour }};">{{ $comment->user->role->name }}</div>
<div class="push_bottom_5">
@if($comment->user->role->id == '1')
<i class="fa fa-star"></i>
@elseif($comment->user->role->id == '2')
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
@elseif($comment->user->role->id == '3')
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
@elseif($comment->user->role->id == '4')
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
@elseif($comment->user->role->id == '5')
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
@elseif($comment->user->role->id >= '6')
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
@endif
</div>
<div class="text-muted text-left"><small>Total Posts: 66</small></div>
<div class="text-muted text-left"><small>Joined 05-11-2014</small></div>
<div class="clerafix"> </div>
</div>
<div class="col-md-5 col-sm-6 col-xs-8">
<div class="clerafix"> </div>
<div class="text-muted"><span class="hidden-md "><i class="fa fa-calendar"></i>
</span><small><span>Posted:</span> {{ date("d-m-Y", strtotime($comment->posted_at)) }}</small></div>
</div>
<div class="col-md-10 col-sm-9 col-xs-12">
<div class="clerafix"> </div>
<div class="content_body"><p>{{ Helper::Filter(BBCode::parse($comment->comment)) }}</p>
</div>
<div class="clerafix"> </div>
</div>
</div>
</div>
<div class="panel-footer clearfix">
<div class="pull-right">
<!-- acties -->
<button class="btn btn-primary btn-sm"><i class="fa fa-edit"></i> Aanpassen</button>
<button class="btn btn-primary btn-sm"><i class="fa fa-quote-left"></i> Quote</button>
<button class="btn btn-danger btn-sm"><i class="fa fa-trash"></i> Verwijder</button>
<button class="btn btn-warning btn-sm"><i class="fa fa-exclamation"></i> Rapporteer</button>
</div>
</div>
</div>
</div>
@endif
@endforeach
<!-- end of comments -->
@if(Auth::check())
<div class="col-sm-offset-1 col-sm-10">
<div class="clearfix"> </div>
<div class="panel panel-default" id="a9">
<div class="panel-heading"><h4>Snelle reactie plaatsen</h4></div>
<div class="panel-body">
{{ Form::open(array('url' => 'SubmitComment')) }}
<input type="hidden" value="{{ $threads->tid }}" name="tid">
{{ Honeypot::generate('spamprt', 'time') }}
<textarea name="comment" class="form-control" col="250" rows="5" placeholder="Typ uw bericht..."></textarea>
<div class="clearfix"> </div>
<button class="btn btn-success pull-left" type="submit" name="action">
Reageer
</button>
{{ Form::close() }}
</div>
</div>
</div>
@endif
<div class="col-md-4 col-md-offset-5">{{ $comments->links() }}</div>
</div>
</div>
@include('globs.footer')
我希望你们能理解我的问题,并且能够找到一个完美的解决方案。
答案 0 :(得分:1)
您似乎总是检索所有条评论。我也可以在您的模板中看到这一点,您可以在@if($comment->tid == $threads->tid)
中将代码包装在foreach
中。
这意味着稍后在模板中,您检索分页链接的位置,它将检索所有条评论的链接,但不是所有评论都显示在页面中,因为您手动过滤它们循环。
查询评论,如
$comments = Comment::orderBy('posted_at', 'asc')
->where('deleted_at', '=', '0000-00-00 00:00:00')
->where('tid', $threads->tid) // it's about this line!
->with('user.role')
->paginate(10);
注意:强> 上面应该为您的问题提供解决方案,但作为额外的,您还可以查看relationships。另外,关于SO "Laravel 4.1: How to paginate eloquent eager relationship?"的答案(因为你用Laravel-4标记了你的帖子)对于如何有效地处理这个问题有一些很好的论据。