Home > database >  How to exclude certain styling in page2 in pagination
How to exclude certain styling in page2 in pagination

Time:02-22

I have followed Jeffery Way in Laravel 8 from scratch amazing series but I'm having a trouble in pagination. In the index page we're making our latest post to have a specific styling and then first 2 posts comes after with a different styling and then rest of posts in different styling. The problem is that these styles are carrying over with us to page2 and 3 and ...etc

I want to make this to only posts in page1 or homepage and when I go to page 2 I should have a default styling for all posts.

This is the index

<x-bloglayout>

    @include('posts.__header')

    @if ($posts->count())
        <x-featuredCard :post="$posts[0]" />

        @if ($posts->count() > 1)

            <div >

                <x-postCard :post="$posts[1]" />
                <x-postCard :post="$posts[2]" />

            </div>

            <div >
                @foreach ($posts->skip(3) as $post)
                    <x-postCard :post="$post" />
                @endforeach
            </div>

        @endif

        {{ $posts->links() }}

    @else
        <p >No posts matches your search, please check back later</p>
    @endif



</x-bloglayout>

I tried to use if directive to say if route is home or ?/page1 do this if not do that but it doesn't seem to work.

This is my pagination:

    public function index()
    {
        return view('posts.index', [
            'posts' => Post::latest()->filter(request(['search', 'category', 'author']))->paginate(6)->withQueryString(),
        ]);
    }

Thanks

CodePudding user response:

You can use currentPage() method on paginator and check if it's on first page. May not be fanciest way but here is an example:

<x-bloglayout>

    @include('posts.__header')

    @if ($posts->count())
    <x-featuredCard :post="$posts[0]" />

    @if ($posts->count() > 1)

    @if($posts->currentPage() == 1)

    <div >

        <x-postCard :post="$posts[1]" />
        <x-postCard :post="$posts[2]" />

    </div>

    @endif

    <div >
        @foreach ($posts->skip($posts->currentPage() == 1 ? 3 : 0) as $post)
        <x-postCard :post="$post" />
        @endforeach
    </div>

    @endif

    {{ $posts->links() }}

    @else
    <p >No posts matches your search, please check back later</p>
    @endif

</x-bloglayout>

You can check Paginator methods from Laravel 8.x document: https://laravel.com/docs/8.x/pagination#paginator-instance-methods

  • Related