Paginate Laravel Collection











up vote
1
down vote

favorite












I have two different querys that I merge to return a collection



$combinados = $histMe->merge($hist)->sortByDesc('created_at');


And I return them this way:



$final = $combinados->all();


But this prints all the collection, how can I paginate this? The paginate() laravel method doesn't work with collections. And how can I controll it from the view?



Thanks!










share|improve this question


























    up vote
    1
    down vote

    favorite












    I have two different querys that I merge to return a collection



    $combinados = $histMe->merge($hist)->sortByDesc('created_at');


    And I return them this way:



    $final = $combinados->all();


    But this prints all the collection, how can I paginate this? The paginate() laravel method doesn't work with collections. And how can I controll it from the view?



    Thanks!










    share|improve this question
























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I have two different querys that I merge to return a collection



      $combinados = $histMe->merge($hist)->sortByDesc('created_at');


      And I return them this way:



      $final = $combinados->all();


      But this prints all the collection, how can I paginate this? The paginate() laravel method doesn't work with collections. And how can I controll it from the view?



      Thanks!










      share|improve this question













      I have two different querys that I merge to return a collection



      $combinados = $histMe->merge($hist)->sortByDesc('created_at');


      And I return them this way:



      $final = $combinados->all();


      But this prints all the collection, how can I paginate this? The paginate() laravel method doesn't work with collections. And how can I controll it from the view?



      Thanks!







      php laravel laravel-blade laravel-collection laravel-paginate






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 10 at 20:47









      Alejandro Paredes

      466




      466
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote













          You can use this code that I wrote a long time ago:



          You must use Length aware paginator:



          use IlluminatePaginationLengthAwarePaginator;


          public function paginate($items, $perPage,$setDefaultOption = true, $options = )
          {
          if($setDefaultOption){
          $options = ['path' => request()->url(), 'query' => request()->query()];
          }
          $page = Input::get('page', 1); // Get the current page or default to 1


          $items = $items instanceof Collection ? $items : Collection::make($items);

          return new LengthAwarePaginator($items->forPage($page, $perPage), $items->count(), $perPage, $page, $options);
          }





          share|improve this answer






























            up vote
            0
            down vote













            The api has changed slightly in newer versions. To paginate a collection you now need to use forPage.



            The method forPage takes two parameters:



            forPage(int $page, int $perPage)


            So you can do something like this in your controller:



            ...
            public function index(Request $request)
            {
            $combinados = $histMe->merge($hist)->sortByDesc('created_at');

            $combinados->forPage($request->get('page'), 25);
            }
            ...





            share|improve this answer





















            • How do I control the pagination in the blade view?
              – Alejandro Paredes
              Nov 10 at 23:10










            • @AlejandroParedes The pagination is controlled by the first argument and the amount of items per page is controlled by the second argument. You can return whatever you want to your blade view. Typically the pagination is controlled by a page url parameter.
              – adam
              Nov 10 at 23:53













            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243254%2fpaginate-laravel-collection%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            You can use this code that I wrote a long time ago:



            You must use Length aware paginator:



            use IlluminatePaginationLengthAwarePaginator;


            public function paginate($items, $perPage,$setDefaultOption = true, $options = )
            {
            if($setDefaultOption){
            $options = ['path' => request()->url(), 'query' => request()->query()];
            }
            $page = Input::get('page', 1); // Get the current page or default to 1


            $items = $items instanceof Collection ? $items : Collection::make($items);

            return new LengthAwarePaginator($items->forPage($page, $perPage), $items->count(), $perPage, $page, $options);
            }





            share|improve this answer



























              up vote
              0
              down vote













              You can use this code that I wrote a long time ago:



              You must use Length aware paginator:



              use IlluminatePaginationLengthAwarePaginator;


              public function paginate($items, $perPage,$setDefaultOption = true, $options = )
              {
              if($setDefaultOption){
              $options = ['path' => request()->url(), 'query' => request()->query()];
              }
              $page = Input::get('page', 1); // Get the current page or default to 1


              $items = $items instanceof Collection ? $items : Collection::make($items);

              return new LengthAwarePaginator($items->forPage($page, $perPage), $items->count(), $perPage, $page, $options);
              }





              share|improve this answer

























                up vote
                0
                down vote










                up vote
                0
                down vote









                You can use this code that I wrote a long time ago:



                You must use Length aware paginator:



                use IlluminatePaginationLengthAwarePaginator;


                public function paginate($items, $perPage,$setDefaultOption = true, $options = )
                {
                if($setDefaultOption){
                $options = ['path' => request()->url(), 'query' => request()->query()];
                }
                $page = Input::get('page', 1); // Get the current page or default to 1


                $items = $items instanceof Collection ? $items : Collection::make($items);

                return new LengthAwarePaginator($items->forPage($page, $perPage), $items->count(), $perPage, $page, $options);
                }





                share|improve this answer














                You can use this code that I wrote a long time ago:



                You must use Length aware paginator:



                use IlluminatePaginationLengthAwarePaginator;


                public function paginate($items, $perPage,$setDefaultOption = true, $options = )
                {
                if($setDefaultOption){
                $options = ['path' => request()->url(), 'query' => request()->query()];
                }
                $page = Input::get('page', 1); // Get the current page or default to 1


                $items = $items instanceof Collection ? $items : Collection::make($items);

                return new LengthAwarePaginator($items->forPage($page, $perPage), $items->count(), $perPage, $page, $options);
                }






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 10 at 21:08

























                answered Nov 10 at 20:59









                Salar Bahador

                4251313




                4251313
























                    up vote
                    0
                    down vote













                    The api has changed slightly in newer versions. To paginate a collection you now need to use forPage.



                    The method forPage takes two parameters:



                    forPage(int $page, int $perPage)


                    So you can do something like this in your controller:



                    ...
                    public function index(Request $request)
                    {
                    $combinados = $histMe->merge($hist)->sortByDesc('created_at');

                    $combinados->forPage($request->get('page'), 25);
                    }
                    ...





                    share|improve this answer





















                    • How do I control the pagination in the blade view?
                      – Alejandro Paredes
                      Nov 10 at 23:10










                    • @AlejandroParedes The pagination is controlled by the first argument and the amount of items per page is controlled by the second argument. You can return whatever you want to your blade view. Typically the pagination is controlled by a page url parameter.
                      – adam
                      Nov 10 at 23:53

















                    up vote
                    0
                    down vote













                    The api has changed slightly in newer versions. To paginate a collection you now need to use forPage.



                    The method forPage takes two parameters:



                    forPage(int $page, int $perPage)


                    So you can do something like this in your controller:



                    ...
                    public function index(Request $request)
                    {
                    $combinados = $histMe->merge($hist)->sortByDesc('created_at');

                    $combinados->forPage($request->get('page'), 25);
                    }
                    ...





                    share|improve this answer





















                    • How do I control the pagination in the blade view?
                      – Alejandro Paredes
                      Nov 10 at 23:10










                    • @AlejandroParedes The pagination is controlled by the first argument and the amount of items per page is controlled by the second argument. You can return whatever you want to your blade view. Typically the pagination is controlled by a page url parameter.
                      – adam
                      Nov 10 at 23:53















                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    The api has changed slightly in newer versions. To paginate a collection you now need to use forPage.



                    The method forPage takes two parameters:



                    forPage(int $page, int $perPage)


                    So you can do something like this in your controller:



                    ...
                    public function index(Request $request)
                    {
                    $combinados = $histMe->merge($hist)->sortByDesc('created_at');

                    $combinados->forPage($request->get('page'), 25);
                    }
                    ...





                    share|improve this answer












                    The api has changed slightly in newer versions. To paginate a collection you now need to use forPage.



                    The method forPage takes two parameters:



                    forPage(int $page, int $perPage)


                    So you can do something like this in your controller:



                    ...
                    public function index(Request $request)
                    {
                    $combinados = $histMe->merge($hist)->sortByDesc('created_at');

                    $combinados->forPage($request->get('page'), 25);
                    }
                    ...






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 10 at 21:14









                    adam

                    456410




                    456410












                    • How do I control the pagination in the blade view?
                      – Alejandro Paredes
                      Nov 10 at 23:10










                    • @AlejandroParedes The pagination is controlled by the first argument and the amount of items per page is controlled by the second argument. You can return whatever you want to your blade view. Typically the pagination is controlled by a page url parameter.
                      – adam
                      Nov 10 at 23:53




















                    • How do I control the pagination in the blade view?
                      – Alejandro Paredes
                      Nov 10 at 23:10










                    • @AlejandroParedes The pagination is controlled by the first argument and the amount of items per page is controlled by the second argument. You can return whatever you want to your blade view. Typically the pagination is controlled by a page url parameter.
                      – adam
                      Nov 10 at 23:53


















                    How do I control the pagination in the blade view?
                    – Alejandro Paredes
                    Nov 10 at 23:10




                    How do I control the pagination in the blade view?
                    – Alejandro Paredes
                    Nov 10 at 23:10












                    @AlejandroParedes The pagination is controlled by the first argument and the amount of items per page is controlled by the second argument. You can return whatever you want to your blade view. Typically the pagination is controlled by a page url parameter.
                    – adam
                    Nov 10 at 23:53






                    @AlejandroParedes The pagination is controlled by the first argument and the amount of items per page is controlled by the second argument. You can return whatever you want to your blade view. Typically the pagination is controlled by a page url parameter.
                    – adam
                    Nov 10 at 23:53




















                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243254%2fpaginate-laravel-collection%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Full-time equivalent

                    Bicuculline

                    さくらももこ