jonassiewertsen / statamic-livewire

A Laravel Livewire integration for Statamics antlers engine
83 stars 14 forks source link

Pagination when using a filter #24

Closed freshface closed 1 year ago

freshface commented 1 year ago

Hi

I need to perform a filter before using the pagination. But when i do got an error: Method Statamic\Entries\EntryCollection::paginate does not exist.

My code:

    {
            $entries = Entry::query()
            ->where('collection', 'projects')
            ->where('locale', Site::current()->handle)
            ->get()
            ->filter(function ($entry, $key) {
                return $this->filters['categories'] ? (array_intersect($entry->get('categories'), $this->filters['categories'])) : true;
                }
            )
            ->filter(function ($entry, $key) {
                return $this->filters['q'] ?  str_contains($entry->get('title'), $this->filters['q']) : true;
        }
        )->paginate(3);

        return $this->withPagination('entries', $entries);
    }

This works:

protected function entries()
    {
            $entries = Entry::query()
            ->where('collection', 'projects')
            ->where('locale', Site::current()->handle)
            ->paginate(3);

        return $this->withPagination('entries', $entries);
    }

But i need to filter the content beforehand. How can i do this?

jonassiewertsen commented 1 year ago

After calling get in the first example, you're getting a different return type than in the example below.

I understand your problem, but this is not related to this addon. You can not paginate a EntryCollection as shown above.