spatie/query-string
Manipulate URLs by building and updating query strings in PHP. Toggle parameters or values, add JSON:API-style filters and sorts, and manage pagination. Useful for generating links and keeping state in apps without manual query parsing.
Request object) and API layers, especially for filtering, pagination, or toggleable features (e.g., dark mode, debug flags).#fragments—could be a gap for single-page apps (SPAs).Request object (e.g., request()->fullUrl()).?param=hello%20world).parse_url, http_build_query).Request methods (e.g., query(), merge()) suffice? If not, why?Validator or Str::of() methods.?sort=desc&limit=10).Request or Form Requests instead.spatie/laravel-query-builder.parse_str, http_build_query) with the package.?debug=true toggling from custom logic to $queryString->toggle('debug').public function handle(Request $request, Closure $next) {
$queryString = new QueryString($request->fullUrl());
// Modify query string logic here
return $next($request);
}
Illuminate\Http\Request via $request->fullUrl().use Spatie\QueryString\QueryString;
$queryString = new QueryString(request()->fullUrl());
$queryString->toggle('feature_flag');
const queryString = new SpatieQueryString(window.location.href);
queryString.toggle('dark_mode');
window.location.href = queryString.toString();
HttpTestCase to assert query string changes:
$response = $this->get('/endpoint?param=value');
$this->assertStringContainsString('param=value', $response->getTargetUri());
composer require spatie/query-string
parse_str, http_build_query) if the package fails.| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Package stops working (PHP 8+) | Query string logic breaks | Fork/replace with native functions |
| URL encoding edge cases | Malformed query strings | Validate inputs with Str::of() |
Integration with Request |
Middleware/controller conflicts | Test thoroughly in staging |
| Frontend JS usage | CORS/CSRF issues | Use Laravel’s signed URLs if needed |
QueryString methods.How can I help you explore Laravel packages today?