Laravel next and prev pagination post












1














In post, when I read post I have two button next post, and prev post. I want do this buttons and redirect user to next post or prev post. How I can do this correctly?



I have url post: http://example.com/post/1



   <div class="entry-navigation">
{{-- <div class="column text-left"><a class="btn btn-outline-secondary btn-sm" href="{{ route('postshow', $offer->id - 1) }}"><i class="icon-arrow-left"></i>&nbsp;Prev</a></div> --}}
<div class="column"><a class="btn btn-outline-secondary view-all" href="{{ route('offers') }}" data-toggle="tooltip" data-placement="top" title="All posts"><i class="icon-menu"></i></a></div>
{{-- <div class="column text-right"><a class="btn btn-outline-secondary btn-sm" href="{{ route('postshow', $offer->id + 1) }}">Next&nbsp;<i class="icon-arrow-right"></i></a></div> --}}
</div>


I think with route id +/- 1 I can do. But how I can check if post not deleted, or if he exists?



I can use and paginate:



$posts = Post::active()->where('id', '!=', $post->id)->paginate(1);


But I get other links with ?page=1 .. ?page=10

How I get full link to next or prev post ?










share|improve this question
























  • I think it's better to use pagination with limit 1 for fetching your resource, then you have access to next and prev link. I you don't want to use pagination, then you should have methods to get prev and next id from your model. otherwise you have to implement many logic on your view.
    – train_fox
    Nov 12 '18 at 10:58










  • @train_fox see please updated question
    – Dumitru
    Nov 12 '18 at 11:02










  • On paginate response what you need are next_page_url and prev_page_url. Don't worry about absolute url cause laravel fills it by your .env config.
    – train_fox
    Nov 12 '18 at 12:16
















1














In post, when I read post I have two button next post, and prev post. I want do this buttons and redirect user to next post or prev post. How I can do this correctly?



I have url post: http://example.com/post/1



   <div class="entry-navigation">
{{-- <div class="column text-left"><a class="btn btn-outline-secondary btn-sm" href="{{ route('postshow', $offer->id - 1) }}"><i class="icon-arrow-left"></i>&nbsp;Prev</a></div> --}}
<div class="column"><a class="btn btn-outline-secondary view-all" href="{{ route('offers') }}" data-toggle="tooltip" data-placement="top" title="All posts"><i class="icon-menu"></i></a></div>
{{-- <div class="column text-right"><a class="btn btn-outline-secondary btn-sm" href="{{ route('postshow', $offer->id + 1) }}">Next&nbsp;<i class="icon-arrow-right"></i></a></div> --}}
</div>


I think with route id +/- 1 I can do. But how I can check if post not deleted, or if he exists?



I can use and paginate:



$posts = Post::active()->where('id', '!=', $post->id)->paginate(1);


But I get other links with ?page=1 .. ?page=10

How I get full link to next or prev post ?










share|improve this question
























  • I think it's better to use pagination with limit 1 for fetching your resource, then you have access to next and prev link. I you don't want to use pagination, then you should have methods to get prev and next id from your model. otherwise you have to implement many logic on your view.
    – train_fox
    Nov 12 '18 at 10:58










  • @train_fox see please updated question
    – Dumitru
    Nov 12 '18 at 11:02










  • On paginate response what you need are next_page_url and prev_page_url. Don't worry about absolute url cause laravel fills it by your .env config.
    – train_fox
    Nov 12 '18 at 12:16














1












1








1







In post, when I read post I have two button next post, and prev post. I want do this buttons and redirect user to next post or prev post. How I can do this correctly?



I have url post: http://example.com/post/1



   <div class="entry-navigation">
{{-- <div class="column text-left"><a class="btn btn-outline-secondary btn-sm" href="{{ route('postshow', $offer->id - 1) }}"><i class="icon-arrow-left"></i>&nbsp;Prev</a></div> --}}
<div class="column"><a class="btn btn-outline-secondary view-all" href="{{ route('offers') }}" data-toggle="tooltip" data-placement="top" title="All posts"><i class="icon-menu"></i></a></div>
{{-- <div class="column text-right"><a class="btn btn-outline-secondary btn-sm" href="{{ route('postshow', $offer->id + 1) }}">Next&nbsp;<i class="icon-arrow-right"></i></a></div> --}}
</div>


I think with route id +/- 1 I can do. But how I can check if post not deleted, or if he exists?



I can use and paginate:



$posts = Post::active()->where('id', '!=', $post->id)->paginate(1);


But I get other links with ?page=1 .. ?page=10

How I get full link to next or prev post ?










share|improve this question















In post, when I read post I have two button next post, and prev post. I want do this buttons and redirect user to next post or prev post. How I can do this correctly?



I have url post: http://example.com/post/1



   <div class="entry-navigation">
{{-- <div class="column text-left"><a class="btn btn-outline-secondary btn-sm" href="{{ route('postshow', $offer->id - 1) }}"><i class="icon-arrow-left"></i>&nbsp;Prev</a></div> --}}
<div class="column"><a class="btn btn-outline-secondary view-all" href="{{ route('offers') }}" data-toggle="tooltip" data-placement="top" title="All posts"><i class="icon-menu"></i></a></div>
{{-- <div class="column text-right"><a class="btn btn-outline-secondary btn-sm" href="{{ route('postshow', $offer->id + 1) }}">Next&nbsp;<i class="icon-arrow-right"></i></a></div> --}}
</div>


I think with route id +/- 1 I can do. But how I can check if post not deleted, or if he exists?



I can use and paginate:



$posts = Post::active()->where('id', '!=', $post->id)->paginate(1);


But I get other links with ?page=1 .. ?page=10

How I get full link to next or prev post ?







php laravel






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 11:02







Dumitru

















asked Nov 12 '18 at 10:54









DumitruDumitru

276112




276112












  • I think it's better to use pagination with limit 1 for fetching your resource, then you have access to next and prev link. I you don't want to use pagination, then you should have methods to get prev and next id from your model. otherwise you have to implement many logic on your view.
    – train_fox
    Nov 12 '18 at 10:58










  • @train_fox see please updated question
    – Dumitru
    Nov 12 '18 at 11:02










  • On paginate response what you need are next_page_url and prev_page_url. Don't worry about absolute url cause laravel fills it by your .env config.
    – train_fox
    Nov 12 '18 at 12:16


















  • I think it's better to use pagination with limit 1 for fetching your resource, then you have access to next and prev link. I you don't want to use pagination, then you should have methods to get prev and next id from your model. otherwise you have to implement many logic on your view.
    – train_fox
    Nov 12 '18 at 10:58










  • @train_fox see please updated question
    – Dumitru
    Nov 12 '18 at 11:02










  • On paginate response what you need are next_page_url and prev_page_url. Don't worry about absolute url cause laravel fills it by your .env config.
    – train_fox
    Nov 12 '18 at 12:16
















I think it's better to use pagination with limit 1 for fetching your resource, then you have access to next and prev link. I you don't want to use pagination, then you should have methods to get prev and next id from your model. otherwise you have to implement many logic on your view.
– train_fox
Nov 12 '18 at 10:58




I think it's better to use pagination with limit 1 for fetching your resource, then you have access to next and prev link. I you don't want to use pagination, then you should have methods to get prev and next id from your model. otherwise you have to implement many logic on your view.
– train_fox
Nov 12 '18 at 10:58












@train_fox see please updated question
– Dumitru
Nov 12 '18 at 11:02




@train_fox see please updated question
– Dumitru
Nov 12 '18 at 11:02












On paginate response what you need are next_page_url and prev_page_url. Don't worry about absolute url cause laravel fills it by your .env config.
– train_fox
Nov 12 '18 at 12:16




On paginate response what you need are next_page_url and prev_page_url. Don't worry about absolute url cause laravel fills it by your .env config.
– train_fox
Nov 12 '18 at 12:16












1 Answer
1






active

oldest

votes


















-1














I'm posting a answer because i i dont have enough reputation.



Here is a link that can help you : Laravel previous and next records



I think its a duplicated question






share|improve this answer





















    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%2f53260629%2flaravel-next-and-prev-pagination-post%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    -1














    I'm posting a answer because i i dont have enough reputation.



    Here is a link that can help you : Laravel previous and next records



    I think its a duplicated question






    share|improve this answer


























      -1














      I'm posting a answer because i i dont have enough reputation.



      Here is a link that can help you : Laravel previous and next records



      I think its a duplicated question






      share|improve this answer
























        -1












        -1








        -1






        I'm posting a answer because i i dont have enough reputation.



        Here is a link that can help you : Laravel previous and next records



        I think its a duplicated question






        share|improve this answer












        I'm posting a answer because i i dont have enough reputation.



        Here is a link that can help you : Laravel previous and next records



        I think its a duplicated question







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 12 '18 at 12:23









        Alberto GuilhermeAlberto Guilherme

        145




        145






























            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%2f53260629%2flaravel-next-and-prev-pagination-post%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

            さくらももこ