[英]Best way to paginate an array of data in laravel and use infinite scroll with vuejs
請有人幫助我,我有一個數據數組(該數組包含一個已登錄用戶的帖子和已登錄用戶朋友的帖子:也就是說,如果我登錄,它將獲取我的帖子和我的朋友的帖子) ,此數組會提取所有帖子,因此,如果我有100個朋友,而我的每個朋友都有30個帖子,我將要獲取300個帖子,我想對獲取的數據進行分頁並從數組中獲取5個帖子,隨着用戶滾動,獲取更多帖子下一頁我正在使用laravel和vuejs來顯示數據,請問什么是實現此目的的最佳方法,請檢查我已經編寫的代碼
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;
}
Vue.js
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
},
}
這里有一個更快的版本,可以檢索帖子。
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;
}
謝謝N69S我終於想到了
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.