Blade + Alpine

laratribe/laravel-advanced-filters v1.0.0 · 80 products · zero build step — Alpine from a CDN, package JS published to public/

NameSKUCategoryStatus PriceStockReleased
Nimbus Pro 16 SKU-0016 Electronics Low 63.70 68 31 Mar 2026
Vertex Mini 17 SKU-0017 Books Out 67.40 81 20 Mar 2026
Lumen Max 18 SKU-0018 Clothing In stock 71.10 94 9 Mar 2026
Atlas Lite 19 SKU-0019 Toys Low 74.80 107 26 Feb 2026
Orbit Pro 20 SKU-0020 Electronics Out 78.50 120 15 Feb 2026
Cobalt Mini 21 Books In stock 82.20 133 4 Feb 2026
Ember Max 22 SKU-0022 Clothing Low 85.90 6 24 Jan 2026
Quartz Lite 23 SKU-0023 Toys Out 89.60 19 13 Jan 2026
Nimbus Pro 24 SKU-0024 Electronics In stock 93.30 32 2 Jan 2026
Vertex Mini 25 SKU-0025 Books Low 97.00 45 22 Dec 2025
Lumen Max 26 SKU-0026 Clothing Out 100.70 58 11 Dec 2025
Atlas Lite 27 SKU-0027 Toys In stock 104.40 71 30 Nov 2025
Orbit Pro 28 Electronics Low 108.10 84 19 Nov 2025
Cobalt Mini 29 SKU-0029 Books Out 111.80 97 8 Nov 2025
Ember Max 30 SKU-0030 Clothing In stock 115.50 110 28 Oct 2025
Showing 16–30 of 80
The code behind this page 3 files

The model below is the same file the Livewire and JSON API pages use. Only the last few lines differ per page — the filtering itself is declared once.

app/Models/Product.php identical on all three pages
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Laratribe\AdvancedFilters\Concerns\HasFilters;
use Laratribe\AdvancedFilters\Contracts\Filterable;
use Laratribe\AdvancedFilters\Filters\{DateFilter, NumericFilter, SetFilter, TextFilter};

class Product extends Model implements Filterable
{
    use HasFilters;

    protected $guarded = [];

    public $timestamps = false;

    protected function casts(): array
    {
        return ['price' => 'float', 'stock' => 'integer', 'released_at' => 'date'];
    }

    /**
     * The one place that decides what can be filtered — and therefore the allow-list.
     * Anything not declared here can never reach the query, whatever the request says.
     */
    public static function filters(): array
    {
        return [
            TextFilter::make('name', 'Name'),
            TextFilter::make('sku', 'SKU')->nullable(),
            SetFilter::make('category', 'Category')
                ->options([
                    'electronics' => 'Electronics',
                    'books' => 'Books',
                    'clothing' => 'Clothing',
                    'toys' => 'Toys',
                ])
                ->multiple(),
            SetFilter::make('status', 'Status')->options([
                'in_stock' => 'In stock',
                'low' => 'Low stock',
                'out' => 'Out of stock',
            ]),
            NumericFilter::make('price', 'Price'),
            NumericFilter::make('stock', 'Stock'),
            DateFilter::make('released_at', 'Released'),
        ];
    }
}
routes/web.php the /blade route
<?php

use App\Models\Product;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use Laratribe\AdvancedFilters\Support\FilteredQuery;

Route::get('/', fn () => view('home'))->name('demo.home');

// ── Blade + Alpine ───────────────────────────────────────────────────────────
Route::get('/blade', function (Request $request) {
    $query = FilteredQuery::for(Product::class)->fromRequest($request);

    return view('blade', [
        'products' => $query->paginate(15),
        'fields' => $query->filterDefinitions(),   // wire contract OUT
        'active' => $query->activeFilters(),      // normalised rows, for the chips
    ]);
})->name('demo.blade');

// ── Livewire ─────────────────────────────────────────────────────────────────
Route::get('/livewire', fn () => view('livewire-page'))->name('demo.livewire');

// ── JSON API (no UI at all) ──────────────────────────────────────────────────
Route::get('/api', fn () => view('api'))->name('demo.api');

Route::get('/api/products/filters', fn () => response()->json([
    'fields' => Product::filterDefinitions(),
]))->name('api.filters');

Route::match(['get', 'post'], '/api/products', fn (Request $request) => response()->json(
    FilteredQuery::for(Product::class)->fromRequest($request)->paginate(15)
))->name('api.products');
resources/views/blade.blade.php this page
@extends('layout')
@section('title', 'Blade + Alpine — Advanced Filters')
@section('heading', 'Blade + Alpine')
@section('frontend', 'zero build step — Alpine from a CDN, package JS published to public/')

@push('head')
    <script type="module">
        import Alpine from 'https://cdn.jsdelivr.net/npm/alpinejs@3.14.1/dist/module.esm.js'
        import '{{ asset('vendor/advanced-filters/advanced-filters.js') }}?v={{ filemtime(public_path('vendor/advanced-filters/advanced-filters.js')) }}'
        window.Alpine = Alpine
        Alpine.start()
    </script>
@endpush

@section('content')
    <div class="card">
        <x-advanced-filters::panel :fields="$fields" :active="$active" :base-url="route('demo.blade')" />
    </div>

    @include('_table')

    @include('_code', [
        'intro' => 'The model below is the <strong>same file</strong> the Livewire and JSON API pages use.
                    Only the last few lines differ per page — the filtering itself is declared once.',
        'files' => [
            ['path' => 'app/Models/Product.php', 'note' => 'identical on all three pages'],
            ['path' => 'routes/web.php', 'note' => 'the /blade route'],
            ['path' => 'resources/views/blade.blade.php', 'note' => 'this page'],
        ],
    ])
@endsection