- How do I set up Eloquent filtering for a Laravel 10 API endpoint?
- First, install the package via Composer (`composer require indexzer0/eloquent-filtering`), then publish the config with `php artisan eloquent-filtering:install`. Add the `Filterable` trait to your model and define allowed filters using `allowedFilters()`. Finally, apply filters in your controller by passing the request array to `Model::filter()`. Example: `$results = Product::filter(request()->query('filter'))->get();`
- Can I use this package with Laravel 9 or earlier?
- No, this package explicitly requires Laravel 10+ and PHP 8.2+. If you’re on an older version, consider alternatives like `spatie/laravel-query-builder` or `baileycommerce/eloquent-filters`, which support broader Laravel versions.
- How does the package handle SQL injection risks?
- The package does not sanitize inputs itself—you must validate filter inputs upstream using Laravel’s built-in validation (e.g., Form Requests) or middleware. Always whitelist allowed fields and types in `allowedFilters()` to prevent malicious queries.
- What’s the performance impact of dynamic filters on large datasets?
- Dynamic filters compile queries at runtime, which adds minimal overhead compared to static scopes. For large datasets, ensure your database is indexed on filtered columns and consider caching frequent filter combinations (e.g., with Laravel’s cache or Redis).
- How do I filter nested relationships or JSON fields (e.g., `options->languages`)?
- The package supports JSON path queries (e.g., `options->languages`) but may introduce performance variability across databases. Test thoroughly in your environment, and ensure your database indexes JSON fields appropriately. For complex cases, consider denormalizing or using database-specific JSON functions.
- Is sorting supported, and how stable is it?
- Sorting is in its infancy and not production-ready. The package prioritizes filtering, so avoid relying on sorting features. For now, handle sorting separately with Laravel’s built-in `orderBy()` or consider extending the package if you need robust sorting.
- Can I integrate this with Laravel Scout for full-text search?
- Direct integration isn’t built-in, but you can combine the package with Scout by first filtering results with `EloquentFiltering` and then applying Scout’s search to the narrowed dataset. Example: `$filtered = Product::filter(request()->query())->scout()->search('query')->get();`
- How do I test filter logic in isolation?
- Use Laravel’s `Model::withoutGlobalScopes()` to bypass global scopes during tests. Mock request data with `Filter::fromArray()` and assert results with `assertDatabaseCount()` or `assertModelMissing()`. Example: `$filter = Filter::fromArray([...]); $results = Product::filter($filter)->get(); $this->assertCount(2, $results);`
- What’s the best way to migrate from custom scopes to this package?
- Start by auditing your models for repetitive scopes (e.g., `scopeActive()`, `scopeByCategory()`). Replace them incrementally with `allowedFilters()` definitions. For example, convert `scopeByCategory($category)` to `Filter::field('category', [FilterType::EQUAL])` in `allowedFilters()`. Test each model thoroughly before full adoption.
- Are there alternatives if I need more advanced filtering (e.g., geospatial, full-text)?
- For geospatial queries, pair this package with Laravel’s `whereRaw()` or use `spatie/laravel-geocoder`. For full-text search, combine with Laravel Scout or `meilisearch/meilisearch-php`. If you need a more feature-rich solution, consider `baileycommerce/eloquent-filters` or `archtechx/tntsearch` for specialized use cases.