Yes, it is possible to have pretty URLs with Livewire 3 wire:model.live. To achieve this, you can use the wire:key directive in your Livewire component.
Here's how you can modify your code to have pretty URLs:
- In your Livewire component, add the
wire:keydirective to the<select>element. This will ensure that Livewire treats each<select>element as a separate component instance.
<select wire:model.live="keywordSlug" wire:key="keywordSlug">
<option value="">Any</option>
@foreach ($webKeywords as $webKeyword)
<option value="{{ $webKeyword->slug }}">{{ ucfirst($webKeyword->plural_name) }}</option>
@endforeach
</select>
- In your routes file, update the route definition for the
/category/{keywordSlug}route to include a name parameter.
Route::get('/category/{keywordSlug}', ShowProducts::class)->name('products.category');
- In your Livewire component, update the
rendermethod to use the named route instead of the route class.
public function render()
{
return view('livewire.show-products', [
'products' => $products,
'webKeywords' => $webKeywords,
])->extends('layouts.app');
}
With these changes, Livewire will update the URL to /category/fruit when you select an option in the <select> element.