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 page") == $i ? 'active' :""}}">
<a
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=¤cy=
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=¤cy=&drive=&location=&make=&max_original_price=&min_original_price=&year=
respectively.
CodePudding user response:
The issue is that your query parameters are null.
fullUrlWithQuery() uses Illuminate\Support\Arr::query().
Illuminate\Support\Arr::query() in turn uses the native http_build_query().
Unfortunately for your use case, http_build_query() will drop parameters which are null:
// returns "test2=data"
http_build_query(["test"=>null,"test2"=>"data"]);
If you want your query parameters which are null to show up in the path you're generating, you'll need to write some custom code in your controller to handle that.
I suggest using the code linked here to parse the URL generated by fullUrl(), adding the page number to the resulting array, setting all null values in the array to the empty string "", then passing the resulting array to http_build_query().
Personally, I think a better pattern is for your controller to be able to handle queries that are missing the parameters you expect.
P.S. The reason fullUrl() doesn't drop the parameters which are null is because it isn't merging arrays, so it doesn't call Arr::query() - code for reference
CodePudding user response:
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 >
<a
href="{{ tap(request(), fn($r) => $r->query->add(['page' => $i]))->fullUrlWithQuery([]) }}">
{{ $i }}
</a>
</li>
@endfor
