I'm implementing infinite pagination into a Livewire component and I'm noticing that on initial page load it's loading objects, but when I scroll and load more data its adding arrays to the collection so I'm ending up with a collection with mixed values of array and object. Any idea how to solve this so that its only objects? The reason I'd prefer to have all objects is I'm using a method in the view which I can't access if it's an array.
/**
* 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();
}
}
The 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
}