I just try to setup the pagination for mc component. I dont wanna use the Builder-Plugin for this, so I tryed to mirror the code within my component.
The first approach worked well: Within the loadItems() method (triggered from onRun()), the following code was performed:
$query = MyModel::where('category_id', 1)->paginate(15);
return $query;
Ofc there was no other limitation. I tried to add those now:
# Published
$query = MyModel::where('published', 1)->get();
$collection = $this->property('collection');
if (!empty($collection) && ((int) $collection === 1))
$query = $query->where('collection', $collection);
$category = $this->property('category');
if (!empty($category)) {
# TODO: Improve query and care about invalid categories
$categoryId = Category::where('slug', $category)->pluck('id')->toArray()[0];
$query = $query->where('category_id', $categoryId);
}
switch ($this->property('sortOrder')):
case 'id_asc':
$query = $query->sortBy('id');
break;
case 'id_desc':
$query = $query->sortByDesc('id');
break;
endswitch;
# Pagination
$recordsPerPage = $this->property('recordsPerPage');
$pageNumber = $this->property('pageNumber');
trace_log($recordsPerPage);
trace_log($pageNumber);
// if ($recordsPerPage > 0) {
// $query = $query->paginate($recordsPerPage);
// }
// $query = $query->take($this->property('recordsPerPage'));
// $pageNumber = trim($this->property('pageNumber'));
// if (!strlen($pageNumber) || !preg_match(
// '/^[0-9]+$/',
// $pageNumber
// )) {
// $pageNumber = 1;
// }
Everything works untill I reach the pagination code and it tells me, the method paginate() does not exist. But I dont find my mistake. Could you help me out here? Thanks a lot