> For the complete documentation index, see [llms.txt](https://docs.opensoutheners.com/oss/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.opensoutheners.com/oss/flex-url/reading-urls/params-and-query-arrays.md).

# toParams() & toQuery()

Read the whole builder state as a nested object/array with toParams(), or as a parse\_str()-shaped array with PHP's toQuery().

## `toParams()`

Returns the whole builder state as a plain nested object/array, mirroring the wire's bracket structure — a single call instead of one reader per bucket.

{% tabs %}
{% tab title="TypeScript" %}

```ts
flexUrl('/posts')
  .filter('status', 'published')
  .filter('due_at', 'gte', '2024-01-01')
  .filter('due_at', 'lte', '2024-01-31')
  .sort('-created_at')
  .page(2)
  .toParams();
// {
//   filter: {
//     status: 'published',
//     due_at: {gte: '2024-01-01', lte: '2024-01-31'},
//   },
//   sort: '-created_at',
//   page: {number: '2'},
// }
```

{% endtab %}

{% tab title="PHP" %}

```php
FlexUrl::make('/posts')
    ->filter('status', 'published')
    ->filter('due_at', 'gte', '2024-01-01')
    ->filter('due_at', 'lte', '2024-01-31')
    ->sort('-created_at')
    ->page(2)
    ->toParams();
// [
//   'filter' => [
//     'status' => 'published',
//     'due_at' => ['gte' => '2024-01-01', 'lte' => '2024-01-31'],
//   ],
//   'sort' => '-created_at',
//   'page' => ['number' => '2'],
// ]
```

{% endtab %}
{% endtabs %}

### Shape rules

* `filter[status]=published` → `{filter: {status: 'published'}}`
* `filter[due_at][gte]=X&filter[due_at][lte]=Y` → `{filter: {due_at: {gte: 'X', lte: 'Y'}}}`
* `sort=a,-b` → `{sort: 'a,-b'}`; `include=a,b` → `{include: 'a,b'}`
* `fields[post]=title,body` → `{fields: {post: 'title,body'}}` (same shape for `appends`)
* `page[number]=1&page[size]=25` → `{page: {number: '1', size: '25'}}`
* `q=term` → `{q: 'term'}`; with search filters → `{q: {value: 'term', filter: {...}}}` (no term → `{q: {filter: {...}}}`)
* Raw/unknown params are set at their own (possibly nested) path.

{% hint style="info" %}
An attribute with both a plain and an operator-keyed entry is promoted to an object/array with the plain value moved under the `''` key (e.g. `{status: {'': 'published', equal: 'archived'}}`) — this only happens if you mix `filter(attr, value)` and `filter(attr, op, value)` for the same attribute, which is unusual but representable.
{% endhint %}

## `toQuery()` (PHP only)

`toQuery()` returns a flat query array in the shape `Illuminate\Http\Request::query()` (i.e. PHP's native `parse_str()`) would hold — nested `[]` params become nested arrays, unlike `toParams()`'s grammar-aware flattening. Useful for building a `Request` to dispatch an in-kernel sub-request, without a hard dependency on `illuminate/http`:

```php
$flexUrl = FlexUrl::make('/api/v1/projects')->filter('status', 'active')->sort('name')->page(2);

$flexUrl->toQuery();
// ['filter' => ['status' => 'active'], 'sort' => 'name', 'page' => ['number' => '2']]
// — same shape Illuminate\Http\Request::query() would hold (built via parse_str()).

$request = Illuminate\Http\Request::create($flexUrl->toRequestUri(), 'GET');
$response = app(Illuminate\Contracts\Http\Kernel::class)->handle($request);
```

{% hint style="info" %}
`toQuery()` has no TypeScript counterpart: it is `parse_str()` semantics — what Laravel itself will see — whereas `toParams()` is grammar-aware. Nothing in a browser asks that question, so the TypeScript package doesn't answer it.
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.opensoutheners.com/oss/flex-url/reading-urls/params-and-query-arrays.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
