'Laravel request()->fullUrlWithQuery() doesn't work as expected

I am trying create a paging on my page using the

request()->fullUrlWithQuery(["page" =>$page_number])

However, instead of getting the full url with query params with the page param appended to it. I get only the path with the page param appended

@for($i = 1; $i<=$pagesCount;$i++)
    <li class="page-item {{request()->get("page") == $i ? 'active' :""}}">
        <a class="page-link"
           href="{{request()->fullUrlWithQuery(["page" =>$i])}}">{{$i}}</a>
    </li>
@endfor

The original url

http://localhost:8899/offers/find?make=&year=&location=&drive=&color=&min_original_price=&max_original_price=&currency=

and when I output:

{{request()->fullUrlWithQuery(["page" =>1])}}
</br>
{{request()->fullUrl()}}
</br>

I get

http://localhost:8899/offers/find?page=1 

and

http://localhost:8899/offers/find?color=&currency=&drive=&location=&make=&max_original_price=&min_original_price=&year=

respectively.



Solution 1:[1]

If this was a named route it would be simpler, but only using request() to build the url, I think you can get this to work with tap()

@for ($i = 1; $i <= $pagesCount; $i++)
  <li class="page-item {{ request()->get('page') == $i ? 'active' : '' }}">
    <a class="page-link"
      href="{{ tap(request(), fn($r) => $r->query->add(['page' => $i]))->fullUrlWithQuery([]) }}">
      {{ $i }}
    </a>
  </li>
@endfor

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 IGP