- How do I install and set up Saring for Laravel?
- Install via Composer with `composer require laraditz/saring`, then add the `Filterable` trait to your Eloquent model. Create a filter class (e.g., `UserFilter`) in `App/Filters` following the `<ModelName>Filter` convention. Define filter methods for each field you want to support, like `name()` or `email()`.
- Does Saring work with Laravel 8 or 9?
- Saring supports Laravel 5.5+ and Lumen, so it works with Laravel 8 and 9. However, test thoroughly in your environment, as the package lacks recent updates. No breaking changes are expected for newer Laravel versions due to its minimal Eloquent integration.
- Can I restrict which fields can be filtered?
- Yes, define a `$filterable` array in your model to whitelist allowed filter fields. For example, `protected $filterable = ['name', 'email'];` ensures only those fields can be filtered via the `filter()` method. This prevents accidental or malicious filtering of sensitive columns.
- How do I handle SQL injection risks with user input?
- Saring does not sanitize inputs by default, so you must validate request data before passing it to `Model::filter()`. Use Laravel’s `Validator` facade or middleware to sanitize inputs. For example, validate with `request()->validate(['name' => 'sometimes|string'])` before calling `filter()`.
- What’s the best way to organize filter classes in a large project?
- The `<ModelName>Filter` convention works for small projects, but for larger ones, consider grouping filters in namespaces like `App/Filters/Users` or `App/QueryFilters`. Override the default filter class location by extending the `Filter` class or configuring a custom path in the trait.
- Can Saring handle complex queries like nested relationships or `orWhere` conditions?
- No, Saring is designed for simple, linear `where()` clauses. For complex queries (e.g., nested relationships, `orWhere`, or multi-condition filters), use Laravel’s native query builder or packages like `spatie/laravel-query-builder`. Saring’s simplicity may become a limitation for advanced filtering needs.
- How do I test filter logic in PHPUnit?
- Mock the `Filter` class in your tests to isolate query logic. For example, use `partialMock()` to test a `UserFilter` without hitting the database. Verify methods like `name()` or `email()` apply the correct `where()` clauses. Test edge cases like empty values or invalid inputs.
- Is there a way to reuse filters across multiple models?
- No, Saring requires a dedicated filter class per model. To share logic, extract common filter methods into a base class or trait (e.g., `SoftDeletesFilter`) and extend it in your model-specific filter classes. This reduces boilerplate but requires manual setup.
- Does Saring support pagination or API resource integration?
- No, Saring focuses solely on query filtering and does not integrate with Laravel’s pagination or API resources. Use `Model::filter($request->all())->paginate()` manually, but note that pagination logic must be handled separately. For API resources, combine with `spatie/laravel-fractal` or similar.
- What are the alternatives to Saring for Eloquent filtering?
- Consider `spatie/laravel-query-builder` for advanced filtering (nested relationships, sorting), `archtechx/tntsearch` for full-text search, or `beberlei/doctrineextensions` for Doctrine-based filtering. For lightweight needs, Saring is simpler but lacks features like validation or caching. Evaluate based on your project’s complexity.