Best way to paginate an array of data in laravel and use infinite scroll with vuejs












0















Please someone should help me out,
i have and array of data (the array contains a logged in users posts and the posts of the logged-in user friends : that is if i log in, it will fetch my posts and my friends posts), this array fetches all post so if i have 100 friends and each of my friend have 30 posts, i will be fetching 300 posts, i want to paginate the fetched data and fetch 5 posts from the array, the fetch more as the user scrolls down the page
am using laravel and vuejs to display the data, pls whats the best way to implement this, check my already written code



public function feeds()
{
$friends = Auth::user()->friends();

$feed = array();

foreach ($friends as $friend):

foreach ($friend->posts as $post):

array_push($feed, $post);

endforeach;

endforeach;


foreach (Auth::user()->posts as $post):

array_push($feed, $post);

endforeach;

usort($feed, function($p1, $p2){
return $p1->id < $p2->id;
});

return $feed;
}


The vuejs



get_feed()
{
this.loading = true;
axios.get('/feed').then((response) => {
this.loading = true;
response.data.forEach((post) => {
this.$store.commit('add_post', post)
this.loading = false;
})
})
}
computed: {
posts() {
return this.$store.getters.all_posts
},
}









share|improve this question





























    0















    Please someone should help me out,
    i have and array of data (the array contains a logged in users posts and the posts of the logged-in user friends : that is if i log in, it will fetch my posts and my friends posts), this array fetches all post so if i have 100 friends and each of my friend have 30 posts, i will be fetching 300 posts, i want to paginate the fetched data and fetch 5 posts from the array, the fetch more as the user scrolls down the page
    am using laravel and vuejs to display the data, pls whats the best way to implement this, check my already written code



    public function feeds()
    {
    $friends = Auth::user()->friends();

    $feed = array();

    foreach ($friends as $friend):

    foreach ($friend->posts as $post):

    array_push($feed, $post);

    endforeach;

    endforeach;


    foreach (Auth::user()->posts as $post):

    array_push($feed, $post);

    endforeach;

    usort($feed, function($p1, $p2){
    return $p1->id < $p2->id;
    });

    return $feed;
    }


    The vuejs



    get_feed()
    {
    this.loading = true;
    axios.get('/feed').then((response) => {
    this.loading = true;
    response.data.forEach((post) => {
    this.$store.commit('add_post', post)
    this.loading = false;
    })
    })
    }
    computed: {
    posts() {
    return this.$store.getters.all_posts
    },
    }









    share|improve this question



























      0












      0








      0








      Please someone should help me out,
      i have and array of data (the array contains a logged in users posts and the posts of the logged-in user friends : that is if i log in, it will fetch my posts and my friends posts), this array fetches all post so if i have 100 friends and each of my friend have 30 posts, i will be fetching 300 posts, i want to paginate the fetched data and fetch 5 posts from the array, the fetch more as the user scrolls down the page
      am using laravel and vuejs to display the data, pls whats the best way to implement this, check my already written code



      public function feeds()
      {
      $friends = Auth::user()->friends();

      $feed = array();

      foreach ($friends as $friend):

      foreach ($friend->posts as $post):

      array_push($feed, $post);

      endforeach;

      endforeach;


      foreach (Auth::user()->posts as $post):

      array_push($feed, $post);

      endforeach;

      usort($feed, function($p1, $p2){
      return $p1->id < $p2->id;
      });

      return $feed;
      }


      The vuejs



      get_feed()
      {
      this.loading = true;
      axios.get('/feed').then((response) => {
      this.loading = true;
      response.data.forEach((post) => {
      this.$store.commit('add_post', post)
      this.loading = false;
      })
      })
      }
      computed: {
      posts() {
      return this.$store.getters.all_posts
      },
      }









      share|improve this question
















      Please someone should help me out,
      i have and array of data (the array contains a logged in users posts and the posts of the logged-in user friends : that is if i log in, it will fetch my posts and my friends posts), this array fetches all post so if i have 100 friends and each of my friend have 30 posts, i will be fetching 300 posts, i want to paginate the fetched data and fetch 5 posts from the array, the fetch more as the user scrolls down the page
      am using laravel and vuejs to display the data, pls whats the best way to implement this, check my already written code



      public function feeds()
      {
      $friends = Auth::user()->friends();

      $feed = array();

      foreach ($friends as $friend):

      foreach ($friend->posts as $post):

      array_push($feed, $post);

      endforeach;

      endforeach;


      foreach (Auth::user()->posts as $post):

      array_push($feed, $post);

      endforeach;

      usort($feed, function($p1, $p2){
      return $p1->id < $p2->id;
      });

      return $feed;
      }


      The vuejs



      get_feed()
      {
      this.loading = true;
      axios.get('/feed').then((response) => {
      this.loading = true;
      response.data.forEach((post) => {
      this.$store.commit('add_post', post)
      this.loading = false;
      })
      })
      }
      computed: {
      posts() {
      return this.$store.getters.all_posts
      },
      }






      php arrays laravel vue.js infinite-scroll






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 '18 at 12:49









      drAlberT

      13.2k42635




      13.2k42635










      asked Nov 22 '18 at 9:20









      BenjaminBenjamin

      12




      12
























          2 Answers
          2






          active

          oldest

          votes


















          0














          here a much faster version to retreive the posts.



          public function feeds(){
          $userdIds = Auth::user()->friends()->pluck('id')->toArray();
          $userIds = Auth::id();

          $feed = Post::whereIn('user_id', $userIds)->orderBy('id', 'desc')->paginate(10);
          return $feed;
          }





          share|improve this answer
























          • Thank You very much though it work, but little modification made it work

            – Benjamin
            Nov 22 '18 at 11:59





















          -1














          Thanks N69S
          i finally came up this



              public function feeds(){
          $userIds = Auth::user()->friends_id();
          array_push($userIds, Auth::id());

          $feed = Posts::whereIn('user_id', $userIds)->orderBy('id', 'desc')->paginate(5);
          return $feed;
          }





          share|improve this answer
























          • Can mark my answer as the answer ? thx

            – N69S
            Nov 22 '18 at 13:18













          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%2f53427514%2fbest-way-to-paginate-an-array-of-data-in-laravel-and-use-infinite-scroll-with-vu%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














          here a much faster version to retreive the posts.



          public function feeds(){
          $userdIds = Auth::user()->friends()->pluck('id')->toArray();
          $userIds = Auth::id();

          $feed = Post::whereIn('user_id', $userIds)->orderBy('id', 'desc')->paginate(10);
          return $feed;
          }





          share|improve this answer
























          • Thank You very much though it work, but little modification made it work

            – Benjamin
            Nov 22 '18 at 11:59


















          0














          here a much faster version to retreive the posts.



          public function feeds(){
          $userdIds = Auth::user()->friends()->pluck('id')->toArray();
          $userIds = Auth::id();

          $feed = Post::whereIn('user_id', $userIds)->orderBy('id', 'desc')->paginate(10);
          return $feed;
          }





          share|improve this answer
























          • Thank You very much though it work, but little modification made it work

            – Benjamin
            Nov 22 '18 at 11:59
















          0












          0








          0







          here a much faster version to retreive the posts.



          public function feeds(){
          $userdIds = Auth::user()->friends()->pluck('id')->toArray();
          $userIds = Auth::id();

          $feed = Post::whereIn('user_id', $userIds)->orderBy('id', 'desc')->paginate(10);
          return $feed;
          }





          share|improve this answer













          here a much faster version to retreive the posts.



          public function feeds(){
          $userdIds = Auth::user()->friends()->pluck('id')->toArray();
          $userIds = Auth::id();

          $feed = Post::whereIn('user_id', $userIds)->orderBy('id', 'desc')->paginate(10);
          return $feed;
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 '18 at 9:31









          N69SN69S

          977514




          977514













          • Thank You very much though it work, but little modification made it work

            – Benjamin
            Nov 22 '18 at 11:59





















          • Thank You very much though it work, but little modification made it work

            – Benjamin
            Nov 22 '18 at 11:59



















          Thank You very much though it work, but little modification made it work

          – Benjamin
          Nov 22 '18 at 11:59







          Thank You very much though it work, but little modification made it work

          – Benjamin
          Nov 22 '18 at 11:59















          -1














          Thanks N69S
          i finally came up this



              public function feeds(){
          $userIds = Auth::user()->friends_id();
          array_push($userIds, Auth::id());

          $feed = Posts::whereIn('user_id', $userIds)->orderBy('id', 'desc')->paginate(5);
          return $feed;
          }





          share|improve this answer
























          • Can mark my answer as the answer ? thx

            – N69S
            Nov 22 '18 at 13:18


















          -1














          Thanks N69S
          i finally came up this



              public function feeds(){
          $userIds = Auth::user()->friends_id();
          array_push($userIds, Auth::id());

          $feed = Posts::whereIn('user_id', $userIds)->orderBy('id', 'desc')->paginate(5);
          return $feed;
          }





          share|improve this answer
























          • Can mark my answer as the answer ? thx

            – N69S
            Nov 22 '18 at 13:18
















          -1












          -1








          -1







          Thanks N69S
          i finally came up this



              public function feeds(){
          $userIds = Auth::user()->friends_id();
          array_push($userIds, Auth::id());

          $feed = Posts::whereIn('user_id', $userIds)->orderBy('id', 'desc')->paginate(5);
          return $feed;
          }





          share|improve this answer













          Thanks N69S
          i finally came up this



              public function feeds(){
          $userIds = Auth::user()->friends_id();
          array_push($userIds, Auth::id());

          $feed = Posts::whereIn('user_id', $userIds)->orderBy('id', 'desc')->paginate(5);
          return $feed;
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 '18 at 12:03









          BenjaminBenjamin

          12




          12













          • Can mark my answer as the answer ? thx

            – N69S
            Nov 22 '18 at 13:18





















          • Can mark my answer as the answer ? thx

            – N69S
            Nov 22 '18 at 13:18



















          Can mark my answer as the answer ? thx

          – N69S
          Nov 22 '18 at 13:18







          Can mark my answer as the answer ? thx

          – N69S
          Nov 22 '18 at 13:18




















          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%2f53427514%2fbest-way-to-paginate-an-array-of-data-in-laravel-and-use-infinite-scroll-with-vu%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

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          TypeError: fit_transform() missing 1 required positional argument: 'X'