Laravel 5.1 分页问题
Laravel 5.1 Pagination issue
我正在使用分页来显示我在数据库中的所有帖子,代码如下
public function index() {
$posts = Posts::where ( 'active', '1' )->orderBy ( 'created_at', 'desc' )->paginate ( 5 );
$title = 'Latest Posts';
return view ( 'home' )->withPosts ( $posts )->withTitle ( $title );
}
工作正常。
但是,我只想显示与某些类别相关的帖子,而不是所有帖子,代码如下
public function cat() {
$uri = $_SERVER ['REQUEST_URI'];
$title = 'Category Posts';
$uri = str_replace ( "/", "", "$uri" );
$posts = Posts::where ( 'category', $uri )->orderBy ( 'created_at', 'desc' )->paginate ( 5 );
/* echo $posts->count(); */
return view ( 'home' )->withPosts ( $posts )->withTitle ( $title );
}
这里不起作用,它只显示第 1 页的 5 个结果,接下来的页面没有显示任何内容。
我也附和了计数,如果该类别中总共有 15 个帖子,则第一页显示 15,但在下一页显示 0
home.blade.php:
@extends('app')
@section('title')
{{$title}}
@endsection
@section('leftContent')
LeftSide content
@endsection
@section('content')
@if (!$posts->count() ) There is no post till now. Login and write a new post now!!!
@else
<div class="">
@foreach( $posts as $post )
<div class="list-group">
<div class="list-group-item s">
<h3>
<a href="{{ url('/'.$post->slug) }}">{{ $post->title }}</a>
@if(!Auth::guest() && ($post->author_id == Auth::user()->id ||
Auth::user()->is_admin())) @if($post->active == '1')
<button class="btn" style="float: right">
<a href="{{ url('edit/'.$post->slug)}}">Edit Post</a>
</button>
@else
<button class="btn" style="float: right">
<a href="{{ url('edit/'.$post->slug)}}">Edit Draft</a>
</button>
@endif @endif
</h3>
<p>
{{ $post->created_at->format('M d,Y \a\t h:i a') }} By <a
href="{{ url('/user/'.$post->author_id)}}">{{ $post->author->name
}}</a>
</p>
<p>
Category :
@if($post->category == 'News')
<a href="{{ url('/news') }}">{{ $post->category }}</a>
@endif
@if($post->category == 'Sports')
<a href="{{ url('/sports') }}">{{ $post->category }}</a>
@endif
@if($post->category == 'Technology')
<a href="{{ url('/technology') }}">{{ $post->category }}</a>
@endif
@if($post->category == 'Other')
<a href="{{ url('/other') }}">{{ $post->category }}</a>
@endif
</p>
<p> Tags:
@foreach (explode(', ',$post->tags) as $tagss) <a href="tags/{{ $tagss }}">{{ $tagss }} </a>
@endforeach
</p>
</div>
<div class="list-group-item">
<article>
{!! str_limit($post->body, $limit = 750, $end = '....... <a
href='.url("/".$post->slug).'>Read More</a>') !!}
</article>
</div><hr style="border-color:#1E90FF";>
</div>
@endforeach {!! $posts->render() !!}
</div>
@endif @endsection @section('rightContent') Right Side content @endsection
我相信解决你问题的唯一方法就是玩
自定义分页器 URI
http://laravel.com/docs/5.1/pagination#basic-usage
也试试这个
public function cat($cat) {
$title = 'Category Posts';
$posts = Posts::where('category', $cat)->orderBy('created_at', 'desc')->paginate(5);
return view ('home')->withPosts($posts)->withTitle($title);
}
我正在使用分页来显示我在数据库中的所有帖子,代码如下
public function index() {
$posts = Posts::where ( 'active', '1' )->orderBy ( 'created_at', 'desc' )->paginate ( 5 );
$title = 'Latest Posts';
return view ( 'home' )->withPosts ( $posts )->withTitle ( $title );
}
工作正常。
但是,我只想显示与某些类别相关的帖子,而不是所有帖子,代码如下
public function cat() {
$uri = $_SERVER ['REQUEST_URI'];
$title = 'Category Posts';
$uri = str_replace ( "/", "", "$uri" );
$posts = Posts::where ( 'category', $uri )->orderBy ( 'created_at', 'desc' )->paginate ( 5 );
/* echo $posts->count(); */
return view ( 'home' )->withPosts ( $posts )->withTitle ( $title );
}
这里不起作用,它只显示第 1 页的 5 个结果,接下来的页面没有显示任何内容。
我也附和了计数,如果该类别中总共有 15 个帖子,则第一页显示 15,但在下一页显示 0
home.blade.php:
@extends('app')
@section('title')
{{$title}}
@endsection
@section('leftContent')
LeftSide content
@endsection
@section('content')
@if (!$posts->count() ) There is no post till now. Login and write a new post now!!!
@else
<div class="">
@foreach( $posts as $post )
<div class="list-group">
<div class="list-group-item s">
<h3>
<a href="{{ url('/'.$post->slug) }}">{{ $post->title }}</a>
@if(!Auth::guest() && ($post->author_id == Auth::user()->id ||
Auth::user()->is_admin())) @if($post->active == '1')
<button class="btn" style="float: right">
<a href="{{ url('edit/'.$post->slug)}}">Edit Post</a>
</button>
@else
<button class="btn" style="float: right">
<a href="{{ url('edit/'.$post->slug)}}">Edit Draft</a>
</button>
@endif @endif
</h3>
<p>
{{ $post->created_at->format('M d,Y \a\t h:i a') }} By <a
href="{{ url('/user/'.$post->author_id)}}">{{ $post->author->name
}}</a>
</p>
<p>
Category :
@if($post->category == 'News')
<a href="{{ url('/news') }}">{{ $post->category }}</a>
@endif
@if($post->category == 'Sports')
<a href="{{ url('/sports') }}">{{ $post->category }}</a>
@endif
@if($post->category == 'Technology')
<a href="{{ url('/technology') }}">{{ $post->category }}</a>
@endif
@if($post->category == 'Other')
<a href="{{ url('/other') }}">{{ $post->category }}</a>
@endif
</p>
<p> Tags:
@foreach (explode(', ',$post->tags) as $tagss) <a href="tags/{{ $tagss }}">{{ $tagss }} </a>
@endforeach
</p>
</div>
<div class="list-group-item">
<article>
{!! str_limit($post->body, $limit = 750, $end = '....... <a
href='.url("/".$post->slug).'>Read More</a>') !!}
</article>
</div><hr style="border-color:#1E90FF";>
</div>
@endforeach {!! $posts->render() !!}
</div>
@endif @endsection @section('rightContent') Right Side content @endsection
我相信解决你问题的唯一方法就是玩
自定义分页器 URI
http://laravel.com/docs/5.1/pagination#basic-usage
也试试这个
public function cat($cat) {
$title = 'Category Posts';
$posts = Posts::where('category', $cat)->orderBy('created_at', 'desc')->paginate(5);
return view ('home')->withPosts($posts)->withTitle($title);
}