Laravel 5.1分页问题

时间:2015-08-14 05:37:40

标签: php pagination laravel-5.1

我使用分页显示数据库中的所有帖子,下面的代码

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 );
}

这里它不起作用,它只显示第一页的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

1 个答案:

答案 0 :(得分:1)

我认为解决问题的唯一方法就是玩

自定义Paginator 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);
}