- How do I add filtering to an Eloquent query with Laravel Purity?
- Simply call `filter()` on your Eloquent query builder. For example, `Post::filter()->get()` enables filtering via URL params like `?filters[title][$contains]=Laravel`. No additional setup is required for basic usage.
- Does Laravel Purity support sorting in addition to filtering?
- Yes, it includes built-in sorting. Use `sort()` alongside `filter()` or configure default sortable fields in your model. Sorting is controlled via URL params like `?sort=-created_at` for descending order.
- Which Laravel versions does Laravel Purity support?
- The package is designed for Laravel 10+ and requires PHP 8.1+. Check the [Packagist page](https://packagist.org/packages/abbasudo/laravel-purity) for the latest version compatibility details.
- Can I restrict which fields are filterable/sortable for security?
- Absolutely. Define `$filterFields` and `$sortFields` in your model to explicitly allow only specific fields. This prevents accidental exposure of sensitive data like passwords or internal IDs.
- How does Laravel Purity handle complex filtering, like nested relations?
- It supports filtering on relations (e.g., `filters[tags.name][$eq]=laravel`) but does not natively handle deeply nested relations like `user.posts.tags.name`. For advanced cases, use custom filters or manual SQL.
- Will this package work with Livewire for real-time filtering?
- Yes, Laravel Purity integrates seamlessly with Livewire. The URL-based query parameters work out of the box, allowing real-time filtering without page reloads in Livewire components.
- Are there performance concerns with many filters applied simultaneously?
- Dynamic query building can introduce SQL complexity. Profile your queries to ensure no N+1 issues arise. Use `select()` or `with()` to optimize large datasets, and restrict filterable fields to minimize overhead.
- How do I test queries with filters and sorting?
- Use Laravel’s testing tools to simulate URL params. For example, `Post::filter()->where('title', 'like', '%Purity%')->get()` mimics a filtered query. Test edge cases like invalid operators or empty filters.
- What happens if an invalid filter operator is used (e.g., `$invalid`)?
- By default, invalid operators are ignored silently. Enable `throw_exceptions` in config to log or handle errors explicitly. Always validate custom filters manually to prevent SQL injection risks.
- Are there alternatives to Laravel Purity for Eloquent filtering?
- Yes, alternatives include `spatie/laravel-query-builder`, `archtechx/telescope-filters`, or custom solutions using `where()` clauses. Laravel Purity stands out for its simplicity, Livewire integration, and expressive URL-based syntax.