towhid's avatar

NEXT PREVIOUS Functionality in Single page view and going to next and previous item when i want .

i have many kind of collection , my favorite list, who favorite me list , my contact pick list , who contact me list . contact trashed list , favorite trash list , many kind of and difference difference type of collections.

now imagine i have 5 items record set in any collection's list Order by date DESC , collections = [ 4,7,9,2,12],

now when i click 7th item then show single details about 7th item .there are two button available

NEXT and PREVIOUS

when i click Next button then show 9th item single page and details about 9th item , and if i click again next Button in 9th number single item page then show 2nd item single page , in this moment if i think i am going to previous item then click previous button and go to 9th number item single page again ....

i hope you are all understand my problem . please suggest me about this functionality .

please guide me practically bu some code of suggestion . if any kind of quarry please ask me -

thank you

0 likes
17 replies
towhid's avatar

hi , @jlrdw

no one understand my post ?? no one ? is this wrote hard word or hard think ?? i don't believe no one understand my question , how much details i share with you ?

its simple -- NEXT and Previous link in single page --- blade . when i click next then move next item if i want i can move previous item also .

this i want - if you don't understand then ask me question what you want to know from me ?

jlrdw's avatar

@towhid Okay here is your exact example:

    public function colPaginate()
    {
        $page = !empty(Request::input('page')) ? Request::input('page') : '1';
        $perpage = "1";
        $offset = ($page - 1) * $perpage;
        //$collection = collect([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
        $collection = collect([4,7,9,2,12]);
        $slice = $collection->slice($offset, 1);
        return view('testarray.pagcol')
                        ->with('slice', $slice)
                        ->with('page', $page);
    }

4,7,9,2,12 just like you

view

        @foreach ($slice as $k => $v)
        {{ $v }} </br>
        @endforeach
        
        
        
            @if($page > 1)
                @php
                $prev = $page - 1; 
                @endphp
                <a href="colpage?page={{ $prev }}">Prev</a>
            @endif
            @php
                $pageno = $page + 1; 
            @endphp
                <a href="colpage?page={{ $pageno }}">next</a>

Works, but my blade isn't the best.

If you don't need to show next if on last page, work out the count and logic for that, it's not that hard.

Sorry I don't know what else you need.

Above shows

7
Prev next

If on page 2.

However for regular queries:

There's simplepaginate, and https://laracasts.com/discuss/channels/guides/paginator-another-episode

Either one can do previous next.

Snapey's avatar

You can use regular paginate with page size of 1?

It depends if you need to show the specific item number in the address bar (for bookmarking).

If this is the case, you will need to pass the array of IDs into the view then index into that array with the current item id and then take index-1 for previous and index+1 as next.

jlrdw's avatar

@snapey does ver 6 now support paginate for collections. Haven't tried yet.

I knew you could using lengthaware, thus I also referred him to https://gist.github.com/vluzrmos/3ce756322702331fdf2bf414fea27bcb And the link of how to do a template for just next and previous, thus the lengthaware would still work.

And is there a problem with my example. It seems to work for a quick next and previous.

jlrdw's avatar

The middle part can easily be removed

Cannot understand what more of an example you would need.

towhid's avatar

@snapey you said

you can use regular paginate With page size of 1 .

but how to do how can please give me example code or give me practical code .

towhid's avatar

Brother @jlrdw

does ver 6 now support paginate for collections.

i have 5.8 version of laravel , and one thing i don't understand your reference link code how to do ?

/**
  * Gera a paginação dos itens de um array ou collection.
  *
  * @param array|Collection      $items
  * @param int   $perPage
  * @param int  $page
  * @param array $options
  *
  * @return LengthAwarePaginator
  */
public function paginate($items, $perPage = 15, $page = null, $options = [])
{
    $page = $page ?: (Paginator::resolveCurrentPage() ?: 1);
    $items = $items instanceof Collection ? $items : Collection::make($items);
    return new LengthAwarePaginator($items->forPage($page, $perPage), $items->count(), $perPage, $page, $options);
}

how to do use ? where i can make previous and next link how ? can i use for-each loop in my single profile ??

jlrdw's avatar

does ver 6 now support paginate for collections.

No

i don't understand your reference link code how to do ?

Look at this to learn more about length aware paginator:

https://laracasts.com/discuss/channels/guides/length-aware-paginator

You still display links in view. There is a learning curve, see above example, but it's a collection use like in gist.

Basically in view you just put the links

@php echo str_replace('/?', '?', $report->render())  @endphp

or if added parameters

@php echo str_replace('/?', '?', $report->appends($parameter_array)->render())  @endphp

It's almost the same as regular paginator, except you have more work and some math to do.

I know you can do it, that gist has helped a lot of others.

jlrdw's avatar

@snapey I asked him was this regular eloquent results and if it was to use regular paginate.

jlrdw's avatar

@towhid this is just this easy, in controller add at top

use Illuminate\Support\Collection;
use Illuminate\Pagination\LengthAwarePaginator;

A controller method just to test:

    public function colPaginate()
    {
        $page = !empty(Request::input('page')) ? Request::input('page') : '1';
        $perPage = "1";
        $offset = ($page - 1) * $perPage;
        //$current_page = LengthAwarePaginator::resolveCurrentPage();
        $current_page = $page;
        $employees = collect([
            ['id' => 4, 'email' => '[email protected]', 'position' => 'Developer'],
            ['id' => 7, 'email' => '[email protected]', 'position' => 'Designer'],
            ['id' => 9, 'email' => '[email protected]', 'position' => 'Developer'],
            ['id' => 2, 'email' => '[email protected]', 'position' => 'boss'],
        ]);

        $current_page_employees = $employees->slice($offset, $perPage)->all();
        $employee = new LengthAwarePaginator($current_page_employees, count($employees), $perPage);

        return view('testarray.pagcol')
                        ->with('employee', $employee);
    }

Just a quick test view:

<!DOCTYPE html>
<html lang="en">
    <head>
      <script src="{{ asset('js/app.js') }}" defer></script>
      <link href="{{ asset('css/app.css') }}" rel="stylesheet"> 
    </head>
    <body>
        @foreach ($employee as $r)
        {{ $r['id'] }}  |  {{ $r['email'] }}  |  {{ $r['position'] }} </br>
        @endforeach
        
        @php echo str_replace('/?', '?', $employee->links('pn')); @endphp
       
        
    </body>    
</html>

Add this code in a file in the view folder, name it pn.blade.php

@if ($paginator->hasPages())
    <nav>
        <ul class="pagination">
            {{-- Previous Page Link --}}
            @if ($paginator->onFirstPage())
                <li class="page-item disabled" aria-disabled="true">
                    <span class="page-link">@lang('pagination.previous')</span>
                </li>
            @else
                <li class="page-item">
                    <a class="page-link" href="{{ $paginator->previousPageUrl() }}" rel="prev">@lang('pagination.previous')</a>
                </li>
            @endif

            {{-- Next Page Link --}}
            @if ($paginator->hasMorePages())
                <li class="page-item">
                    <a class="page-link" href="{{ $paginator->nextPageUrl() }}" rel="next">@lang('pagination.next')</a>
                </li>
            @else
                <li class="page-item disabled" aria-disabled="true">
                    <span class="page-link">@lang('pagination.next')</span>
                </li>
            @endif
        </ul>
    </nav>
@endif

Gives the exact next and previous.

It is really that easy.

Restyle links as you wish.

Please if you need links restyled and need help please start a new topic for that.

towhid's avatar

Thanks @snapey for full support from you ,. but bad news is when i am trying to multi pagination in my project its so complex because of already 80 % user panel user profile Details ... i f change i cannot fix out easily ... that's why now its pause this feature . i will try again .. next 2nd week ,

towhid's avatar

Dear brother @jlrdw

you are too much helpful person . i know that i will follow your instruction as mu local any project then i will understand its solve my problem or not .. give me time . i will update soon thank you after next 2nd week because now time the feature developing pause . thank you

Please or to participate in this conversation.