Laravel 11 Livewire 3 Query Strings for State Management

Websolutionstuff | Mar-13-2025 | Categories : Laravel

In this tutorial, I will show you how to use query strings in Laravel 11 Livewire 3 to maintain state across page reloads. Query strings allow us to persist component properties in the URL, making it easier to share links with specific search queries, filters, or pagination states.

Let’s see how to implement this feature step by step.

Laravel 11 Livewire 3 Query Strings for State Management

Laravel 11 Livewire 3 Query Strings for State Management

 

Step 1: Install Livewire in Laravel 11

If you haven’t installed Livewire yet, install it using Composer:

composer require livewire/livewire

 

Step 2: Create a Livewire Component

Generate a Livewire component named SearchUsers:

 

Step 3: Add Query String Support to the Component

Open app/Livewire/SearchUsers.php and update the code:

namespace App\Livewire;

use Livewire\Component;
use App\Models\User;

class SearchUsers extends Component
{
    public $search = '';

    // Enable query string for the search property
    protected $queryString = ['search'];

    public function render()
    {
        $users = User::where('name', 'like', '%' . $this->search . '%')->get();

        return view('livewire.search-users', compact('users'));
    }
}

Here’s what happens:

  • The $queryString property makes search persist in the URL (?search=John).
  • When you refresh the page, Livewire will read the query string and keep the search term.

 

Step 4: Update the Blade View

Open resources/views/livewire/search-users.blade.php and add the following code:

<div>
    <input type="text" wire:model.debounce.500ms="search" placeholder="Search users..." class="form-control">

    <ul>
        @foreach($users as $user)
            <li>{{ $user->name }}</li>
        @endforeach
    </ul>
</div>

wire:model.debounce.500ms="search" ensures that Livewire waits 500ms after typing before updating the query string.

 

Step 5: Use the Livewire Component in a Blade View

Now, include the Livewire component in a Blade view, like resources/views/welcome.blade.php:

@livewire('search-users')

 

Step 6: Test the Query String Feature
  • Visit http://127.0.0.1:8000 and type in the search box.
  • You’ll see the URL update with ?search=John (if you type "John").
  • Refresh the page, and the search term remains!
  • You can also share the URL, and the search result will be preloaded.

 


You might also like:

Recommended Post
Featured Post
How to Search Object by ID and Remove It from JSON Array In JavaScript
How to Search Object by ID and...

In this example we will see how to search object by id and remove it from json array in javascript. we need to add ...

Read More

May-26-2021

How to Upload File on the FTP Server Using PHP
How to Upload File on the FTP...

In this small post i will show you how to upload file on the ftp server using php. As we know there are many ftp functio...

Read More

May-20-2021

Feature Flag System in Laravel 11 for Controlled Releases
Feature Flag System in Laravel...

Hello, laravel web developers! In this tutorial, I will show you how to build a feature flag system in Laravel 11 that a...

Read More

Oct-09-2024

Laravel 11 Summernote Image Upload Example
Laravel 11 Summernote Image Up...

Hello, laravel web developers! In this article, we'll see how to upload images in the Summernote text editor in...

Read More

May-17-2024