Franzzy's avatar

Pagination

Hi,so i have an app with catalog. And when you click on filter it makes history push state and add param to get query. And it looks like this :

category?filter-1[]=2000&filter-1[]=1500&filter-1[]=2500 . 

So i dicided to add default laravel pagination.

$products->withQueryString()->links()

And... pagination links look like this

filter-1[0]=1500&filter-1[1]=2000&filter-1[2]=2500&filter-1[3]=3000&filter-1[4]=3500&page=2

So, is there any workaround to set get query as is?

0 likes
3 replies
vainway 's avatar

@franzzy your codes are kinda confusing please show well your codes and we can help you to solve your problem.

Franzzy's avatar

@niyo Okay i got it. So i have a page with url:

http://site.test/catalog/category

And when i'm adding filter with ajax,it becomes:

http://site.test/catalog/category?filter-1[]=1500&filter-1[]=2000

It work fine.No problems here.

But then i've decided to add default laravel pagination.

$products->withQueryString()->links()

And it's giving me

http://site.test/catalog/category?filter-1[0]=1500&filter-1[1]=2000&page=2

For some reason laravel is looping throw my get array and adding indexes. So is there any way to add my get params without changing them. I meas as.

anilkumarthakur60's avatar

i think you are using spatie query builder

    $products = QueryBuilder::for(Product::class)
            ->allowedFilters(
                [
                    AllowedFilter::exact('brand', 'brand_id'), //&filter[brand]=1,2,3,                    
                    AllowedFilter::exact('user', 'user_id'), //&filter[user]=1,2,3,4
                    AllowedFilter::exact('category', 'category_id'), //&filter[category]=1,2,3,4                    
                    AllowedFilter::scope('search'), //filter[search]=any_word
                ],
            )
            ->allowedSorts(['name', 'id'])->paginate(20)
            ->appends(request()->query());

your url whould be like

/category?&filter[category]=3,4,5,6,&filter[brand]=1,2,3,4&filter[user]=1,2,3

Please or to participate in this conversation.