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
If you haven’t installed Livewire yet, install it using Composer:
composer require livewire/livewire
Generate a Livewire component named SearchUsers:
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:
$queryString
property makes search
persist in the URL (?search=John
).
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.
Now, include the Livewire component in a Blade view, like resources/views/welcome.blade.php:
@livewire('search-users')
http://127.0.0.1:8000
and type in the search box.?search=John
(if you type "John").
You might also like:
In this example we will see how to search object by id and remove it from json array in javascript. we need to add ...
May-26-2021
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...
May-20-2021
Hello, laravel web developers! In this tutorial, I will show you how to build a feature flag system in Laravel 11 that a...
Oct-09-2024
Hello, laravel web developers! In this article, we'll see how to upload images in the Summernote text editor in...
May-17-2024