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 '18 at 20:47









      Alejandro ParedesAlejandro Paredes

      498




      498






















          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 '18 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 '18 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 '18 at 21:08

























                answered Nov 10 '18 at 20:59









                Salar BahadorSalar Bahador

                4151313




                4151313























                    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 '18 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 '18 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 '18 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 '18 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 '18 at 21:14









                    adamadam

                    917811




                    917811












                    • How do I control the pagination in the blade view?

                      – Alejandro Paredes
                      Nov 10 '18 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 '18 at 23:53


















                    • How do I control the pagination in the blade view?

                      – Alejandro Paredes
                      Nov 10 '18 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 '18 at 23:53

















                    How do I control the pagination in the blade view?

                    – Alejandro Paredes
                    Nov 10 '18 at 23:10





                    How do I control the pagination in the blade view?

                    – Alejandro Paredes
                    Nov 10 '18 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 '18 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 '18 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.




                    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

                    𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

                    Crossroads (UK TV series)

                    ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế