$ show spatie/laravel-query-builder
This is a PHP library for Laravel that provides a fluent API to build Eloquent queries from HTTP request query parameters, enabling filtering, sorting, including relationships, and selecting fields. It is implemented as a wrapper around Laravel's Eloquent Builder, parsing query string parameters like 'filter', 'sort', 'include', and 'fields' to dynamically modify the underlying query. The architecture consists of a central QueryBuilder class that delegates to specialized concerns (FiltersQuery, SortsQuery, AddsIncludesToQuery, AddsFieldsToQuery) and uses a set of pluggable filter, sort, and include implementations that implement defined interfaces. External dependencies are limited to Laravel's illuminate packages and spatie/laravel-package-tools. The library is widely used and well-tested, with extensive documentation.
The LLM identified 4 files worth highlighting for reuse:
Implements a filter that groups multiple AllowedFilter instances under a single URL parameter with explicit OR or AND conjunction. Members can be any filter type and the shorthand value is broadcast to every member. This enables complex querying patterns like ?filter[q]=John to search across multiple columns with OR logic. The implementation is self-contained, only depending on the Filter interface and AllowedFilter class, and can be extracted for use in other projects needing grouped filter logic.
A reusable trait that provides logic for detecting and handling relation properties in filters (e.g., 'posts.title'). It uses dot-notation to parse the relation and applies the filter within a whereHas clause. This trait is used by FiltersExact, FiltersPartial, and FiltersOperator, and can be reused in custom filter implementations that need to support filtering on related model attributes.
Handles eager loading of relationships including nested ones using dot notation. It also integrates with field selection by setting a fields callback that restricts selected columns on the related tables. The logic for building the nested 'with' array and handling field constraints is non-trivial and self-contained, making it a good reference for implementing similar include mechanisms in other ORM wrappers.
A simple sort implementation that applies an orderBy clause. While straightforward, it serves as a clean example of the Sort interface and can be reused or extended. Its isolation and clarity make it a reusable component for projects needing custom sorting logic.