Query String

Sometimes it's useful to update the browser's query string when your component state changes.

For example, if you were building a "search posts" component, and wanted the query string to reflect the current search value like so:


This way, when a user hits the back button, or bookmarks the page, you can get the initial state out of the query string, rather than resetting the component every time.

In these cases, you can add a property's name to protected $queryString, and Livewire will update the query string every time the property value changes, and also update the property when the query string changes.

class SearchPosts extends Component
    public $search;

    protected $queryString = ['search'];

    public function render()
        return view('livewire.search-posts', [
            'posts' => Post::where('title', 'like', '%'.$this->search.'%')->get(),
    <input wire:model="search" type="search" placeholder="Search posts by title...">

    <h1>Search Results:</h1>

        @foreach($posts as $post)
            <li>{{ $post->title }}</li>

Keeping A Clean Query String

In the case above, when the search property is empty, the query string will look like this:


There are other cases where you might want to only represent a value in the query string if it is NOT the default setting.

For example, if you have a $page property to track pagination in a component, you may want to remove the page property from the query string when the user is on the first page.

In cases like these, you can use the following syntax:

class SearchPosts extends Component
    public $foo;
    public $search = '';
    public $page = 1;

    protected $queryString = [
        'search' => ['except' => ''],
        'page' => ['except' => 1],

← Previous Topic

File Downloads

Next Topic →