- How do I install Laravel Purity and enable filtering on a model?
- Run `composer require abbasudo/laravel-purity` and use the `Filterable` trait on your model. Then call `Post::filter()->get()` in your controller. The package automatically parses URL query parameters like `filters[title][$contains]=Laravel` to apply filters.
- Does Laravel Purity support Laravel 10+ and Eloquent relationships?
- Yes, it’s fully compatible with Laravel 10+ and supports relational filtering (e.g., `filters[tags.name][$eq]=Laravel`). The package handles nested relations via Eloquent’s query builder, but deeply complex queries may require manual SQL overrides.
- Can I restrict filtering to specific fields for security?
- Absolutely. Use the `$filterFields` array in your model or publish the config with `php artisan vendor:publish --tag=purity` to whitelist allowed fields. This prevents arbitrary table/column access via URL parameters.
- How does Laravel Purity integrate with Livewire or SPAs?
- For Livewire, bind `$filters` to your component and let the package handle the rest. SPAs (React/Vue) use URL query strings like `?filters[price][$gt]=100`, which work seamlessly with libraries like `qs` or Axios interceptors.
- Will this package slow down my queries for large datasets?
- Performance depends on your database indexes and query complexity. Start with a pilot endpoint to test. For large datasets, consider adding `remember()` to cache results or optimizing with database-specific features like PostgreSQL full-text search.
- How do I add custom filters (e.g., date ranges, full-text search)?
- Extend the `Filterable` trait or use model macros to define custom logic. For example, add a `dateRange` filter method to handle `filters[created_at][$between]=2023-01-01,2023-12-31`. Document these in your API specs for frontend teams.
- Does Laravel Purity work with GraphQL or only REST APIs?
- It’s designed for REST APIs with URL-based filtering. For GraphQL, you’d need to manually translate client queries (e.g., Apollo variables) into the `filters[field][operator]` syntax or build a custom resolver layer.
- How do I handle malformed or malicious filter inputs?
- The package validates inputs against whitelisted fields, but always sanitize dynamic values. For extra security, log suspicious queries (e.g., `filters[password][$eq]=...`) and use Laravel’s `validate()` middleware to block unsafe parameters.
- Can I migrate from custom filtering logic to Laravel Purity incrementally?
- Yes. Replace `Post::where(...)->get()` with `Post::filter()->get()` in controllers. Update frontend clients to use the new query syntax (e.g., `?filters[status][$in]=active,published`). Use feature flags to roll out changes gradually.
- Are there alternatives to Laravel Purity for filtering in Laravel?
- Yes. Consider `spatie/laravel-query-builder` for advanced SQL queries, `laravel-scout` for search, or `baileyherbert/laravel-query-filter` for a simpler syntax. Laravel Purity stands out for its URL-based, frontend-friendly approach and Livewire integration.