Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Saring Laravel Package

laraditz/saring

Saring is a simple Eloquent model filtering package for Laravel and Lumen. Add the Filterable trait to models, create per-model filter classes (e.g., UserFilter), optionally whitelist filterable fields, and call Model::filter($request->all()).

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Pros:
    • Lightweight and focused on a single, well-defined use case (Eloquent model filtering).
    • Leverages Laravel’s native Eloquent query builder, ensuring consistency with existing query patterns.
    • MIT-licensed, reducing legal/licensing concerns.
    • Minimal abstraction over Eloquent, making it easy to debug and extend.
  • Cons:
    • Limited maturity: No stars, no clear adoption, and minimal documentation (README + changelog).
    • No built-in validation: Filter inputs are directly passed to where() clauses, risking SQL injection if not sanitized externally.
    • No support for complex queries: Lacks features like nested filters, relationships, or pagination integration.
    • Hardcoded filter class naming convention (<ModelName>Filter) may not align with existing codebases.

Integration Feasibility

  • Low-risk for simple use cases: Ideal for basic LIKE-based or exact-match filtering (e.g., search bars, dropdown filters).
  • Requires manual setup: No auto-discovery; filters must be explicitly defined per model.
  • Compatibility:
    • Works with Laravel 5.5+ and Lumen (PHP 7.1+).
    • No database-specific dependencies (pure Eloquent).
  • Testing effort: Minimal, as it’s a thin wrapper over Eloquent queries.

Technical Risk

  • Security: No input sanitization or validation by default. Requires application-level protection (e.g., Laravel’s Validator).
  • Performance: No query optimization (e.g., orWhere vs. where logic, index usage). Poorly designed filters could degrade performance.
  • Maintenance debt: Custom filter classes proliferate with each model, increasing boilerplate.
  • Future-proofing: Package lacks active development (no recent commits, no roadmap).

Key Questions

  1. Does the team already use a filtering pattern? If yes, does this package reduce duplication vs. existing solutions (e.g., custom traits or packages like spatie/laravel-query-builder)?
  2. Are filter inputs trusted? If user-provided, how will SQL injection be mitigated (e.g., via Laravel’s Validator or whereIn/whereRaw)?
  3. Will filters grow complex? If nested relationships or multi-condition filters are needed, this package may become a bottleneck.
  4. How will filter classes be organized? The <ModelName>Filter convention may not fit large codebases (e.g., App/Filters/UserFilter vs. App/QueryFilters/User).
  5. Is there a need for caching or query reuse? This package doesn’t support caching filtered queries or reusable filter sets.

Integration Approach

Stack Fit

  • Best for:
    • Laravel/Lumen applications with simple, read-heavy filtering needs (e.g., admin dashboards, search interfaces).
    • Teams already using Eloquent and wanting to avoid heavy packages (e.g., Scout, Query Builder).
  • Poor fit for:
    • Applications requiring complex filtering (e.g., faceted search, nested relationships).
    • High-performance APIs where query optimization is critical.
    • Projects needing built-in validation or API resource integration.

Migration Path

  1. Pilot Phase:
    • Start with 1–2 non-critical models (e.g., Product, User).
    • Implement filter classes alongside existing queries to compare performance and maintainability.
  2. Incremental Adoption:
    • Replace ad-hoc where() clauses in controllers with Saring filters.
    • Example:
      // Before
      $users = User::where('name', 'LIKE', '%John%')->get();
      
      // After
      $users = (new UserFilter)->apply(request()->query)->get();
      
  3. Refactor Controllers:
    • Centralize filter logic in filter classes (moving away from scattered where() calls).
    • Use middleware to parse and apply filters (e.g., FilterMiddleware).

Compatibility

  • Laravel/Lumen: Works out-of-the-box with Eloquent models.
  • Customizations:
    • Override Filter class to add global features (e.g., default scopes, query logging).
    • Extend with traits for shared filter logic (e.g., SoftDeletesFilter).
  • Testing:
    • Mock Filter classes in unit tests to isolate query logic.
    • Test edge cases (e.g., empty filter values, malformed input).

Sequencing

  1. Setup:
    • Install via Composer.
    • Create App/Filters directory and define filter classes.
  2. Controller Integration:
    • Inject filter classes into controllers or use a service layer.
    • Example:
      public function index(UserFilter $filter)
      {
          return $filter->apply(request()->query)->get();
      }
      
  3. API/Route Integration:
    • For APIs, bind filter classes to routes (Laravel 8+ auto-resolution).
    • Example:
      Route::get('/users', [UserController::class, 'index'])->middleware(FilterMiddleware::class);
      
  4. Documentation:
    • Add filter usage to API specs (e.g., OpenAPI/Swagger).
    • Document filter parameters for frontend teams.

Operational Impact

Maintenance

  • Pros:
    • Filter logic is encapsulated in model-specific classes, improving readability.
    • Easy to modify or extend individual filters without touching controllers.
  • Cons:
    • Boilerplate: Each model requires a filter class, increasing file count.
    • Dependency on package: If laraditz/saring is abandoned, filters may need rewriting.
    • No built-in testing utilities: Manual test coverage required for filter logic.

Support

  • Debugging:
    • Queries are standard Eloquent, so debugging tools (e.g., Laravel Debugbar, toSql()) work as usual.
    • Filter classes can be logged for troubleshooting.
  • Troubleshooting:
    • Common issues:
      • Incorrect where() syntax in filter methods.
      • Missing database indexes on filtered columns.
      • Input sanitization oversights.
    • Workarounds:
      • Use whereRaw for complex conditions.
      • Add validation in filter methods (e.g., if ($value) $this->where(...)).

Scaling

  • Performance:
    • Bottlenecks:
      • Unindexed columns in LIKE queries.
      • N+1 queries if filters don’t use with() for relationships.
    • Mitigations:
      • Add database indexes for filtered columns.
      • Use select() to limit returned fields.
      • Cache frequent filter combinations (e.g., remember()).
  • Throughput:
    • Lightweight, but not optimized for high-concurrency scenarios (no connection pooling or query batching).

Failure Modes

Failure Scenario Impact Mitigation
SQL injection (unsanitized input) Data corruption or exposure Validate inputs in controllers or filters.
Missing database indexes Slow queries, timeouts Add indexes; monitor query performance.
Filter class errors Broken queries, 500 errors Unit test filters; use try-catch in controllers.
Package abandonment Stranded technical debt Fork or rewrite critical filters.
Overuse of LIKE Poor performance on large datasets Use whereIn or full-text search for complex queries.

Ramp-Up

  • Developer Onboarding:
    • Time: 1–2 hours to understand the pattern (trait + filter class).
    • Documentation Needs:
      • Example filter classes for common use cases (e.g., date ranges, relationships).
      • Guidelines for input sanitization and validation.
  • Team Adoption:
    • Resistance Points:
      • New boilerplate (filter classes) may feel redundant if teams already use ad-hoc queries.
      • Requires discipline to centralize filter logic.
    • Adoption Strategies:
      • Start with a single team or feature.
      • Showcase reduced controller clutter as a win.
      • Pair with a code review checklist for filter implementations.
  • Training:
    • Workshops: Demo how to migrate from where() clauses to filters.
    • Templates: Provide starter filter class templates for common models (e.g., User, Post).
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
croct/coding-standard
croct/plug-php
nqxcode/phpmorphy
boundwize/pyrameter
testo/facade
headercat/phpstan-extension-ide-helper
yosymfony/parser-utils
innmind/black-box
babenkoivan/elastic-migrations
babenkoivan/elastic-adapter
develia/commons
dmstr/symfony-system-resources-bundle
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
renatomarinho/laravel-page-speed
develia/geo-bundle
austinheap/laravel-database-encryption
dreamzy/livewire-charts
touchestate-sdk/php-sdk
22h/doctrine-garbage-collection-bundle