https://github.com/marcorieser/statamic-live-search
A Statamic Live Search realized with Laravel Livewire.
https://github.com/marcorieser/statamic-live-search
laravel-livewire statamic statamic-addon
Last synced: 8 months ago
JSON representation
A Statamic Live Search realized with Laravel Livewire.
- Host: GitHub
- URL: https://github.com/marcorieser/statamic-live-search
- Owner: marcorieser
- Created: 2021-01-28T22:53:47.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2025-03-24T10:56:06.000Z (about 1 year ago)
- Last Synced: 2025-04-02T06:08:41.993Z (about 1 year ago)
- Topics: laravel-livewire, statamic, statamic-addon
- Language: PHP
- Homepage:
- Size: 294 KB
- Stars: 20
- Watchers: 3
- Forks: 14
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
Awesome Lists containing this project
README
# Statamic Live Search

[](https://packagist.org/packages/marcorieser/statamic-live-search)
> A Statamic Live Search realised with [Laravel Livewire](https://laravel-livewire.com/).
It's fast to implement, hooks into the Statamic 3 core search and updates search results as you type.

This Package extends my third-party [Statamic 3 Livewire integration](https://github.com/marcorieser/statamic-livewire).
## No need for live search?
Check out my [Statamic 3 Livewire integration](https://github.com/marcorieser/statamic-livewire).
## Upgrading
Check out the upgrade guide: [Upgrade Guide](#upgrade-guide)
## Installation
Pull in the package with composer
```bash
composer require marcorieser/statamic-live-search
```
## Requirements
- PHP >= 8.1
- Laravel 10 | 11 | 12
- Statamic 4 | 5
## Set up Livewire
The option to create your first search provides a quick-start example to get you going.
As the statamic-live-search extends the statamic-livewire addon, the setup is exactly the same and a deeper understanding might be useful. See the link below for more information.
[Statamic 3 Livewire integration Docs](https://github.com/marcorieser/statamic-livewire#general-documentation)
## Create your first search
Add the `livewire:search` component to one of your templates and define your template.
```html
...
{{ livewire:styles }}
{{ livewire:search }}
...
{{ livewire:scripts }}
...
@livewireStyles
...
@livewireScripts
```
## Setup the template
### Use the default dropdown layout
To get you started as fast as possible, we provide a default template. You can publish it and edit it according to your needs.
```bash
php artisan vendor:publish --tag=live-search:views
```
After publishing, you will find the template inside `resources/views/vendor/live-search/dropdown.blade.php`. It can be edited as you like.
### Use your own template
Create your own template and put it anywhere you like. Define the template in the tag and you are ready to go.
If you need augmented values - as in the case of images - it's easiest to use Antlers, so you don't need to worry about it.
```html
{{ livewire:search template='partials.your-own-search-template' }}
```
If the template name is `partials.search`, the template is expected at `resources\views\partials\search.blade.php` or `resources\views\partials\search.antlers.html`.
This might be a solid starting point for your own template:
#### Blade
```html
@forelse($results as $result)
- {{ $result['title'] }}
@empty
No matches found
@endforelse
```
#### Antlers
```html
{{ results }}
- {{ title }}
{{ /results }}
```
## Configure your index
This is the best bit. This addon hooks into Statamic core search. Just configure your indexes in the `config/statamic/search.php` file.
If you don't provide an index we will search everything. That's great for smaller sites.
A more specific search could look something like this:
```php
'blog' => [
'driver' => 'local',
'searchables' => 'collection:blog',
'fields' => ['title'],
],
```
Remember to define the index in your component:
```html
{{ livewire:search template='partials.search' index='blog' }}
```
To update your indexes run `php please search:update` [More information](https://statamic.dev/search#updating-indexes)
[See the Statamic docs for more information](https://statamic.dev/search#searchables)
## Customize the search logic
The parts we have provided have been built in a modular fashion so you can easily extend them if you wish.
### Extend the Search Class
#### 1. Create your own Livewire Controller
`php artisan make:livewire YourCustomLivewireController`
#### 2. Extend the Search class
```php
namespace App\Your\Namespace;
use MarcoRieser\LiveSearch\Http\Livewire\Search;
class YourCustomLivewireController extends Search
{
public $template;
public $index;
public function mount(string $template, string $index = null)
{
// You can pass these as parameters or they can be hardcoded.
$this->template = $template;
$this->index = $index;
}
public function render()
{
// Do your stuff here.
return view($this->template, [
'results' => $this->search($this->q, $this->index),
]);
}
}
```
#### 2. Use the SearchFacade Controller
It might be that you want to customize the name of the query string or that you want to use multiple filters.
You can import the SearchFacade trait and write a complete Livewire Controller as you need it.
`use MarcoRieser\LiveSearch\Traits\SearchFacade;`
The method we have provided expects the following parameters:
`search($query, ?string $index = null, ?int $limit = 10)`
Have fun customizing.
## Upgrade guide
### Version 1 to 2
Livewire will be updated to Version 3 under the hood. A full Livewire upgrade guide can be found here: https://livewire.laravel.com/docs/upgrading
**Breaking change**
Use `wire:model.live` in your template, instead of `wire:model`
> In Livewire 3, wire:model is "deferred" by default (instead of by wire:model.defer). To achieve the same behavior as wire:model from Livewire 2, you must use wire:model.live.
https://livewire.laravel.com/docs/upgrading#wiremodel
## Credits
Thanks to:
- [Caleb](https://github.com/calebporzio) and the community for building [Livewire](https://laravel-livewire.com/)
- [Austenc](https://github.com/austenc) for the marketplace preview image
## Support
I love to share with the community. Nevertheless, it does take a lot of work, time and effort.
[Sponsor me on GitHub](https://github.com/sponsors/marcorieser/) to support my work and this addon.
## License
This plugin is published under the MIT license. Feel free to use it and remember to spread the love.