eddy1992's avatar

Pagination problem

Hi I have appended query string in the url which looks like this

localhost:8000/catalog?brand_id[]=5&brand_id[]=10&brand_id[]=1

Now my Model that is creating the pagination is

 $products = Product::whereHas('brands', function($query) use ($brandIds)
                        {
                            $query->whereIn('brand_id', $brandIds);
                        })->paginate(16);

Now the pagination gets created in the front end but the problem is it creates pagination like this

http://localhost:8000/catalog?page=3

But now I want the pagination to be working with the query string like this

localhost:8000/catalog?brand_id[]=5&brand_id[]=10&brand_id[]=1&page=3

How will I achieve this kind of pagination. Please assist. Thank you

0 likes
2 replies
bbojan83's avatar
$brandIds = Request::all();

$products = Product::whereHas('brands', function($query) use ($brandIds) {
    $query->whereIn('brand_id', $brandIds);
})->paginate(1);

// make path
$path = 'catalog?';

foreach ($brandIds['brand_id'] as $id) {
    $path .= 'brand_id[]=' . $id . '&';
}

$products->setPath($path);

return $products;
bbojan83's avatar

Or something like this:

$brandIds = Request::all();

$products = Product::whereHas('brands', function($query) use ($brandIds) {
    $query->whereIn('brand_id', array_collapse($brandIds));
})->paginate(16);

$products->appends($brandIds);

return view('catalog', compact('products'));

Please or to participate in this conversation.