Have problem with pagination after using filters

Hi to all,

I have controller that working with form filters. But when im filter content pagination isn’t change
For example I have pagination limit 10 items per page, 100 page items, so we have 10 page in pagination. After filtering i have only 40 page items. But pagination is still showing 10 pages. How can I fix it?

<?php


    return function($site, $pages, $page) {

      // get all betting guides and add pagination
      $links = page('betting-links')->children()->visible()->paginate(10)->flip();
      
      if($query_rating = get('rating')) {
      		$links = $links->search($query_rating, 'rating');
    	}
      if($query_category = get('category')) {
      		$links = $links->search($query_category, 'site_category');
    	}
      if($query_language = get('language')) {
      		$links = $links->search($query_language, 'site_language');
    	}

      // create a shortcut for pagination
      $pagination = $links->pagination();

      // pass $guides and $pagination to the template
      return compact('links', 'query_rating', 'query_category', 'query_language', 'pagination');

    };

You need to paginate the collection after filtering:

<?php


    return function($site, $pages, $page) {

      // get all betting guides and add pagination
      $links = page('betting-links')->children()->visible()->flip();
      
      if($query_rating = get('rating')) {
      		$links = $links->search($query_rating, 'rating');
    	}
      if($query_category = get('category')) {
      		$links = $links->search($query_category, 'site_category');
    	}
      if($query_language = get('language')) {
      		$links = $links->search($query_language, 'site_language');
    	}
      
      $links->paginate(10);
      
      // create a shortcut for pagination
      $pagination = $links->pagination();

      // pass $guides and $pagination to the template
      return compact('links', 'query_rating', 'query_category', 'query_language', 'pagination');

    };

but how to show basic pagination (all items list?) if I paginate after filtering, i will see all items list (without pagingation) before filtering.

No, you paginate $links. If none of the filters applies, $links are all the pages, if a filter applies, $links is a collection of the filtered pages.

Here is working version for me $links = $links->paginate(10);

return function($site, $pages, $page) {

  // get all betting guides and add pagination
  $links = page('betting-links')->children()->visible()->flip();
  
  if($query_rating = get('rating')) {
  		$links = $links->search($query_rating, 'rating');
	}
  if($query_category = get('category')) {
  		$links = $links->search($query_category, 'site_category');
	}
  if($query_language = get('language')) {
  		$links = $links->search($query_language, 'site_language');
	}
  
  $links = $links->paginate(10);
  
  // create a shortcut for pagination
  $pagination = $links->pagination();

  // pass $guides and $pagination to the template
  return compact('links', 'query_rating', 'query_category', 'query_language', 'pagination');

};
1 Like