Best way to paginate an array of data in laravel and use infinite scroll with vuejs
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
add a comment |
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
add a comment |
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
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
php arrays laravel vue.js infinite-scroll
edited Nov 22 '18 at 12:49
drAlberT
13.2k42635
13.2k42635
asked Nov 22 '18 at 9:20
BenjaminBenjamin
12
12
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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;
}
Thank You very much though it work, but little modification made it work
– Benjamin
Nov 22 '18 at 11:59
add a comment |
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;
}
Can mark my answer as the answer ? thx
– N69S
Nov 22 '18 at 13:18
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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;
}
Thank You very much though it work, but little modification made it work
– Benjamin
Nov 22 '18 at 11:59
add a comment |
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;
}
Thank You very much though it work, but little modification made it work
– Benjamin
Nov 22 '18 at 11:59
add a comment |
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;
}
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;
}
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
add a comment |
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
add a comment |
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;
}
Can mark my answer as the answer ? thx
– N69S
Nov 22 '18 at 13:18
add a comment |
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;
}
Can mark my answer as the answer ? thx
– N69S
Nov 22 '18 at 13:18
add a comment |
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;
}
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;
}
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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