Paginate() dont work as expected

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 :slight_smile:

Did you mean to use get() on the first line? I think you want to modify the query before you finally paginate.

2 Likes

@axomat is correct, it should not include the get() method if the expectation is to return a query…

$query = MyModel::where('published', 1);

The code you have is returning a collection.

$collection = MyModel::where('published', 1)->get();

Which has similar methods available to a database query, but not paginate() where the process fails.

1 Like

Thanks a lot you two, with your help I could solve the problem.

I had another bug: Since I used get(), my $query was basically a Collection which I then used the sortBy() method on, but this only works on Collections, too, right?

So when I removed the get() method, the sortBy() method didnt work anymore and I had to change it to orderBy()

So in case someone has the same problem:
Here’s the final code:

        # Published
        $query = Head::where('published', 1);

        # Collection
        $collection = $this->property('collection');
        if (!empty($collection) && ((int) $collection === 1))
            $query = $query->where('collection', $collection);
[...]
        # Sorting
        switch ($this->property('sortOrder')):
            case 'id_asc':
                $query = $query->orderBy('id');
                break;
            case 'id_desc':
                $query = $query->orderBy('id', 'desc');
                break;
        endswitch;

        # Pagination
        $recordsPerPage = $this->property('recordsPerPage');
        $query = $query->paginate($recordsPerPage);

        return $query;

So just to ensure:
$query = MyModel::where(); => Returns Query
$collection = Model:where()->get() => Returns Collection

$query = DB::table(…)->where(); => Returns Query
$collection = DB::table(…)->where()->get() => Returns Collection

->sortBy(’…); => only works on collections
->orderBy(’…’, ‘desc’); => only works on queries