【发布时间】:2022-01-30 06:47:49
【问题描述】:
我在 Livewire 组件中实现了无限分页,我注意到在初始页面加载时它正在加载对象,但是当我滚动并加载更多数据时,它会将数组添加到集合中,所以我最终得到了一个集合具有数组和对象的混合值。知道如何解决这个问题,以便它唯一的对象吗?我希望拥有所有对象的原因是我在视图中使用了一个方法,如果它是一个数组,我就无法访问它。
/**
* Loads more blog posts.
*
* @return void
*/
public function loadBlogs()
{
if ($this->hasMorePages !== null && !$this->hasMorePages) {
return;
}
if ($this->category_id) {
$posts = BlogPost::where('user_id', request()->user()->id)
->where('category_id', $this->category_id)
->with('category')
->orderBy('id', 'desc')
->cursorPaginate(16, ['*'], 'cursor', Cursor::fromEncoded($this->nextCursor));
} else {
$posts = BlogPost::where('user_id', request()->user()->id)
->with('category')
->orderBy('id', 'desc')
->cursorPaginate(16, ['*'], 'cursor', Cursor::fromEncoded($this->nextCursor));
}
$this->blogPosts->push(...$posts->items());
$this->hasMorePages = $posts->hasMorePages();
if ($this->hasMorePages === true) {
$this->nextCursor = $posts->nextCursor()->encode();
}
}
dd:
Illuminate\Support\Collection {#1751 ▼
#items: array:32 [▼
0 => array:10 [▶]
1 => array:10 [▶]
2 => array:10 [▶]
3 => array:10 [▶]
4 => array:10 [▶]
5 => array:10 [▶]
6 => array:10 [▶]
7 => array:10 [▶]
8 => array:10 [▶]
9 => array:10 [▶]
10 => array:10 [▶]
11 => array:10 [▶]
12 => array:10 [▶]
13 => array:10 [▶]
14 => array:10 [▶]
15 => array:10 [▶]
16 => App\Models\BlogPost {#1775 ▶}
17 => App\Models\BlogPost {#1776 ▶}
18 => App\Models\BlogPost {#1777 ▶}
19 => App\Models\BlogPost {#1778 ▶}
20 => App\Models\BlogPost {#1779 ▶}
21 => App\Models\BlogPost {#1780 ▶}
22 => App\Models\BlogPost {#1781 ▶}
23 => App\Models\BlogPost {#1782 ▶}
24 => App\Models\BlogPost {#1783 ▶}
25 => App\Models\BlogPost {#1784 ▶}
26 => App\Models\BlogPost {#1785 ▶}
27 => App\Models\BlogPost {#1786 ▶}
28 => App\Models\BlogPost {#1787 ▶}
29 => App\Models\BlogPost {#1788 ▶}
30 => App\Models\BlogPost {#1789 ▶}
31 => App\Models\BlogPost {#1790 ▶}
]
#escapeWhenCastingToString: false
}
【问题讨论】:
标签: laravel pagination laravel-livewire laravel-collection