PHP code example of kirschbaum-development / livewire-filters

1. Go to this page and download the library: Download kirschbaum-development/livewire-filters library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?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');
    }
}
bash
php artisan vendor:publish --tag=livewire-filters-views
bash
php artisan vendor:publish --tag=livewire-filters-config