...
Laravel, Tutorials

How to apply pagination in tag posts in Laravel

19 December 2018

Laravel pagination

The paginate method automatically takes care of setting the proper limit and offset based on the current page being viewed by the user. By default, the current page is detected by the value of the page query string argument on the HTTP request.
In this example, the only argument passed to the paginate method is the number of items you would like displayed “per page”. In this case, let’s specify that we would like to display tag’s 4 posts per page.

1) First we need to define relation between tags and posts

Tag model (Tags and posts relation using model) [folder path : app\Tag.php]

public function posts() {
    return $this->morphedByMany('App\Post','taggable')->approved();
}

2) Second we need to find tag detail from tag id then get that tag posts with pagination using below code.

PostController (fatch tag posts with pagination) [folder path : app\Http\Controllers\PostController.php]

public function tag_posts($id) {
    $tag = Tag::findOrFail($id);
    $posts = $tag->posts()->orderBy('created_at', 'desc')->paginate(4);
    $name = $tag->name;
    return view('front.tag-posts', compact('posts', 'name'));
}

3) Tag posts view page.

tag-posts (tag posts view file) [folder path : resources\views\front\tag-posts.blade.php]

<!-- Items -->
<div class="posts-items blog-page">
    <div class="row masonry-container portfolioContainer">
        <!-- End -->
        @foreach($posts as $post)
        <!-- Item-1 -->
        <div class="post-item gallery_product col-12 col-lg-3 col-sm-6 wow fadeInUp filter-item 
                   {{ $post->categories['title'] }}" data-cat="{{ $post->categories['title'] }}" 
                  data-wow-offset="-100">
            <!-- Image -->
            <div class="post-image col-12">
                <!-- Overlay -->
                <a href="{{ route('single-blog', ['id' => $post->id]) }}">
                    <div class="post-overlay">
                        <i class="fab fa-readme"></i>
                    </div>
                </a>
                <!-- End -->
                <img src="{{ asset('admin/images/'.App\Media::where(['id' => $post->image])->first()->thumb) }}" alt"">
            </div>
            <!-- End -->
            <!-- Category -->
            <div class="post-category">
                <a href="{{ route('show-category-posts', ['name'=>$post->categories['slug'],'id' => $post->categories['id']]) }}">
                    {{ $post->categories['title'] }}
                </a>
            </div>
            <!-- End -->
            <!-- Name -->
            <div class="post-name">
                <a href="{{ route('single-blog', ['id' => $post->id]) }}">
                    {{ $post->title }}
                </a>
            </div>
            <!-- End -->
            <!-- Description -->
            <div class="post-description">
                {!! Helper::words($post->body, 10,'....') !!}
            </div>
            <!-- End -->
            <!-- Date -->
            <div class="post-date">
                {{ Carbon\Carbon::parse($post->created_at)->format('M d Y') }}
            </div>
            <!-- End -->
        </div>
        <!-- End -->
        @endforeach
    </div>
</div>
<!-- End Items -->     
<!-- Pagination -->
<div class="pagination">            
    {!! $posts->links('front.vendor.pagination') !!}            
</div>
<!-- End Pagination -->

4) Pagination page design.

pagination.blade.php (pagination file) [folder path : views\front\vendor\pagination.blade.php]

@if ($paginator->hasPages())
<ul>
    {{-- Previous Page Link --}}
    @if ($paginator->onFirstPage())
    @else
        <li><a href="{{ $paginator->previousPageUrl() }}" rel="prev">Previous</a></li>
    @endif
        {{-- Pagination Elements --}}
        @foreach ($elements as $element)
            {{-- "Three Dots" Separator --}}
            @if (is_string($element))
                <li class="disabled"><span>{{ $element }}</span></li>
            @endif
            {{-- Array Of Links --}}
            @if (is_array($element))
                @foreach ($element as $page => $url)
                    @if ($page == $paginator->currentPage())
                        <li class="active"><span>{{ $page }}</span></li>
                    @else
                        <li><a href="{{ $url }}">{{ $page }}</a></li>
                    @endif
                @endforeach
            @endif
        @endforeach
        {{-- Next Page Link --}}
        @if ($paginator->hasMorePages())
        <li><a href="{{ $paginator->nextPageUrl() }}" rel="next">Next</a></li>
        @else
        @endif
    </ul>
@endif

Leave a Comments