<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
kirschbaum-development / livewire-filters example snippets
use HasFilters;
public function filters(): array
{
return [
Filter::make('title'),
Filter::make('type')->options(['text', 'link', 'audio', 'video'])->default(['audio']),
Filter::make('status')->options(['published', 'draft'])->default('published'),
];
}
// Helper tFilteredValue('type');
// Using the accessor
$this->filters['type']->value();
// Using the property directly
$this->filters['type']->value;
use App\Models\Post;
use Kirschbaum\LivewireFilters\Filter;
use Kirschbaum\LivewireFilters\HasFilters;
use Livewire\Component;
class PostsList extends Component
{
use HasFilters;
public function filters(): array
{
return [
Filter::make('type')->options(['text', 'link', 'audio', 'video'])->default(['text', 'link']),
];
}
public function getPostsProperty()
{
return Post::query()
->when($this->getFilterValue('type'), fn ($query, $values) => $query->whereIn('type', $values))
->paginate();
}
public function render()
{
return view('livewire.posts-list', [
'filterCount' => $this->filterCount,
'isFiltered' => $this->isFiltered,
'posts' => $this->posts,
]);
}
}
use Kirschbaum\LivewireFilters\FilterComponent;
class DateFilter extends FilterComponent
{
public function render()
{
return view('livewire.filters.date-filter');
}
}