Reputation: 3
I am using a pagination for showing all my posts from database ,the code below
public function index() {
$posts = Posts::where ( 'active', '1' )->orderBy ( 'created_at', 'desc' )->paginate ( 5 );
$title = 'Latest Posts';
return view ( 'home' )->withPosts ( $posts )->withTitle ( $title );
}
work fine.
But , instead of all posts , i want to show only posts related to some category , the code below
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 );
}
here it doesnt work , it is showing only 1st page's 5 results , the next pages are not showing anything.
i also echoed the count , if there are total 15 posts in that category , it is displaying 15 in first page , but in next pages it is displaying 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
Upvotes: 0
Views: 423
Reputation: 3784
I believe the only wat to solve your problem is to play with
Customizing The Paginator URI
http://laravel.com/docs/5.1/pagination#basic-usage
also try this
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);
}
Upvotes: 1