- How do I enable filtering on a Laravel Eloquent model?
- Add the `Filterable` trait to your model and define a `$filterable` array with allowed fields (e.g., `['name', 'role.name']`). Relationships use dot notation. No extra configuration is needed for basic filtering.
- What’s the difference between v1 and v2 of this package?
- v2 requires filter parameters to be namespaced under `filters[]` (e.g., `?filters[name]=John`). v1 used flat keys like `?name=John`. If upgrading, update your API routes and client requests to match the new format.
- Can I use custom operators like `LIKE` or `IN` with this package?
- Yes, but only for fields without custom filter methods. The package auto-handles operators like `eq`, `gte`, `lte`, `neq`, and `in`. For `LIKE` or other custom logic, define a custom filter method and handle the operator manually.
- How do I filter nested relationships (e.g., `user.role.department`)?
- Use dot notation in the `$filterable` array (e.g., `['role.department']`). The package will automatically traverse the relationship chain and apply the filter to the nested attribute.
- Does this package support OR conditions between filters?
- No, all filters are combined with AND logic by default. For OR conditions, you’ll need to manually chain queries or use Laravel’s `orWhere` after applying the filter.
- How do I validate filter inputs before applying them?
- The package doesn’t include built-in validation. Use Laravel’s `Validator` or `FormRequest` to validate the `filters[]` input before passing it to `Model::filter()`. Example: `Validator::validate($request->all(), ['filters.*' => 'sometimes|string'])`.
- Will this package work with Laravel 8 or older versions?
- No, it requires **PHP 8.1+** and **Laravel 9–13**. If you’re on an older version, consider alternatives like `spatie/laravel-query-builder` or manually implementing filtering logic.
- How do I optimize performance for deep relationship filters?
- Use Laravel’s `with()` or `load()` to eager-load relationships before filtering. Example: `User::with('role.department')->filter($request->all())`. Without eager loading, deeply nested filters risk N+1 query issues.
- Can I restrict filter access (e.g., only admins can filter `sensitive_field`)?
- Yes, implement middleware or gates to validate user permissions before calling `Model::filter()`. Example: `if (!auth()->user()->isAdmin()) { unset($request->filters['sensitive_field']); }`
- What are some alternatives to this package for Laravel filtering?
- Consider `spatie/laravel-query-builder` (more flexible but complex), `beberlei/doctrine-extensions` (for Doctrine users), or `laravel-scout` (for search-specific needs). This package is ideal if you want a lightweight, Eloquent-native solution.