Paginate Laravel Collection










1














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


























    1














    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
























      1












      1








      1







      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

      518




      518






















          2 Answers
          2






          active

          oldest

          votes


















          0














          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






























            0














            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',
            autoActivateHeartbeat: false,
            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









            0














            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



























              0














              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

























                0












                0








                0






                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























                    0














                    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
















                    0














                    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














                    0












                    0








                    0






                    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

                    917811




                    917811











                    • 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
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid …


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid …


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    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

                    Top Tejano songwriter Luis Silva dead of heart attack at 64

                    政党

                    天津地下鉄3号線