Laravel Multiple Pagination in one page

后端 未结 10 1692
独厮守ぢ
独厮守ぢ 2020-11-27 16:15

I\'m having some trouble with my pagination. I\'m having two tables with data from a database and I paginated it with laravel Paginator.

Now my problem is when you g

相关标签:
10条回答
  • 2020-11-27 16:56

    The answer of anonym may be extended to be very handy in the views that have related models. tested in laravel-5.4:

    Suppose we have a view for the CustomerController show.blade.php and we pass the Customer model to it using:

    ```

    return view(['customer' => \App\Customer::find($customerId)]);
    

    ``` Each Customer has many Product, and has many Location and we want list with pagination both his/her Products and Locations, simply we may do it in the view like:

    @foreach($customer->products()->paginate(10,['*'],'productsPage') as $product)
    HTML list
    @endforeach
    {!! $customer->products()->paginate(10,['*'],'productsPage')->links() !!}
    
    @foreach($customer->locations()->paginate(10,['*'],'locationsPage') as $location)
    HTML list
    @endforeach
    {!! $customer->locations()->paginate(10,['*'],'locationsPage')->links() !!}
    
    0 讨论(0)
  • 2020-11-27 16:58

    This is late, but it works with laravel 7.x

    $messages = Message::where('status', 0)
        ->paginate(10, ['*'], 'p')
        ->appends(Arr::except(Request::query(), 'p'));
    

    In your view, example(inbox.blade.php)

    {{$messages->links()}}
    
    0 讨论(0)
  • 2020-11-27 17:01

    This is an easy solution I've found on Laravel 4.

    Controller

    Change the page name before you make the paginator:

    Paginator::setPageName('page_a');
    $collection_A = ModelA::paginate(10);
    
    Paginator::setPageName('page_b');
    $collection_B = ModelB::paginate(10);
    

    View

    Do the same: change the page name before you print the links

    Paginator::setPageName('page_a');
    $collection_A->links();
    
    Paginator::setPageName('page_b');
    $collection_B->links();
    

    If you don't want to lose any page state while you navigate to another page, append to the links the current page from all collections:

    Paginator::setPageName('page_a');
    $collection_A->appends('page_b', Input::get('page_b',1))->links();
    
    Paginator::setPageName('page_b');
    $collection_B->appends('page_a', Input::get('page_a',1))->links();
    
    0 讨论(0)
  • 2020-11-27 17:08
    $publishedArticles = Article::paginate(10, ['*'], 'published');
    $unpublishedArticles = Article::paginate(10, ['*'], 'unpublished');
    

    The third argument is used as follows:

    laravel/public/articles?published=3

    laravel/public/articles?unpublished=1

    0 讨论(0)
  • 2020-11-27 17:12

    in laravel 5.3 (maibe working in other laravel 5 version), i use like this:

        $produk = Produk::paginate(5, ['*'], 'produk');
        $region = Region::paginate(5, ['*'], 'region');
    

    and in blade templating append links currents of other to keep the position:

        {{$produk->appends(['region' => $region->currentPage()])->links()}}    
        {{$region->appends(['produk' => $produk->currentPage()])->links()}}    
    
    0 讨论(0)
  • 2020-11-27 17:12

    Sounds to me like you need to update the paging tool so that it has an extra param that identifies the table as it's not smart enough to know that it might have 2 instances of itself on the same page.. DOH!

    Rather than having to set the current page for all tables through a URL, ideally you want to store the active page number in the session by table id THEN, your script only has to worry about instructions and not worry about ensuring it carries existing page numbers also.

    Here's a really draft concept....

    // The page would be $tableId + "," + $pageNum
    // Passed as a value of "page"
    $goTo = "$tableId,$pageNum";
    ?page=$goTo
    

    Then, when it gets to the part that's getting your table data it would do something like

    if(!empty($_GET["page"])){
        list($tableId,$pageNum) = explode(",",$_GET["page"]);
    
        // Store table's active page number
        $_SESSION["_tableBrowser"][$tableId] = $pageNum;
    
    
        // Your table reader would then look at the session for the active page number
        // and not what is in $_GET
    }
    
    0 讨论(0)
提交回复
热议问题