andyandy's avatar

Remove ?page=1 from paginate()

I had this problem:

blah.com/articles //my main article page
blah.com/articles?page=1 //is duplicate with previous page
blah.com/articles?page=2
blah.com/articles?page=3

So i published pagination template and used this preg_replace to remove ?page=1

//previous button link
<a class="page-link" href="{{preg_replace('/\?'.$paginator->getPageName().'=[1]$/','', $paginator->previousPageUrl())}}" rel="prev" aria-label="@lang('pagination.previous')">&lsaquo;</a>

//numbered page links
<li class="page-item"><a class="page-link" href="{{preg_replace('/\?'.$paginator->getPageName().'=[1]$/','',$url)}}">{{ $page }}</a></li>

It worked well, when I was using:

$records->links();

Problem is, it will stop working when I use:

$records->appends(request()->input())->links()
0 likes
2 replies
SilenceBringer's avatar
Level 55

Hi @andyandy I think the problem is that you search for string like ?page=1, but with append params to the links it can be ?foo=bar&page=1, so, no leading ? before page param

I think you need to look for ?page=1$ (at the end of string) OR &page=1 OR page=1&

Please or to participate in this conversation.