# spatie/laravel-query-builder architecture

> Architecture diagram of the GitHub repository spatie/laravel-query-builder, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-01-17). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/spatie/laravel-query-builder
- Repository: https://github.com/spatie/laravel-query-builder

## Overview

##### Main Components

a. Core Query Builder Layer
- Extension of Laravel's Eloquent Builder
- QueryBuilderRequest component that handles incoming requests

b. Feature Modules (arrange these as separate but connected components)
- Filtering System
- Sorting System
- Including Relations System
- Field Selection System

c. Supporting Components
- Exception Handling Layer
- Service Provider
- Configuration

##### Component Relationships

**[HTTP Request] → [QueryBuilderRequest]**
- ↓
**[Configuration] → [QueryBuilder Core] ← [Laravel Eloquent Builder]**
- ↓
- ┌───────┬───────┬───────┬───────┐
- ↓       ↓       ↓       ↓       ↓
- [Filters] [Sorts] [Includes] [Fields] [Exceptions]
- ↓       ↓       ↓       ↓
- [Database Layer]

##### Detailed Component Breakdown

a. QueryBuilder Core:
- Connections to AllowedFilter, AllowedSort, AllowedInclude classes
- Concerns traits that modify queries (AddsFieldsToQuery, FiltersQuery, etc.)

b. Filters Module:
- Filter types (FilterExact, FilterPartial, FilterCallback, etc.)
- Relationship with FilterOperator enum

c. Sorts Module:
- SortsField and SortsCallback
- To SortDirection enum

d. Includes Module:
- IncludedRelationship, IncludedCallback, IncludedCount
- Relationship loading capabilities

##### Implementation Details

- Use UML notation for class relationships
- Methods and properties for main components
- Configuration options from config/query-builder.php
- Points of extension (custom filters, sorts, includes)

##### Notes

- Keep the focus on the query building process
- How HTTP query parameters flow through the system
- Where custom logic can be injected
- Package's integration with Laravel's existing features

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Request
        HTTP[HTTP Request]
        QBR[QueryBuilderRequest]:::core
    end

    subgraph Core
        QB[QueryBuilder]:::core
        LEB[Laravel Eloquent Builder]
        Config[Configuration]:::config
    end

    subgraph "Feature Modules"
        direction TB
        subgraph Filters
            direction TB
            F[Filtering System]:::feature
            FQ[FiltersQuery]:::feature
            FT[Filter Types]:::feature
            FO[Filter Operators]:::feature
            AF[AllowedFilter]:::feature
        end

        subgraph Sorts
            direction TB
            S[Sorting System]:::feature
            SQ[SortsQuery]:::feature
            ST[Sort Types]:::feature
            SD[Sort Direction]:::feature
            AS[AllowedSort]:::feature
        end

        subgraph Includes
            direction TB
            I[Including Relations]:::feature
            IQ[AddsIncludesToQuery]:::feature
            IT[Include Types]:::feature
            II[Include Interface]:::feature
            AI[AllowedInclude]:::feature
        end

        subgraph Fields
            direction TB
            FS[Field Selection]:::feature
            FQ2[AddsFieldsToQuery]:::feature
        end
    end

    subgraph Support
        SP[Service Provider]:::support
        EH[Exception Handler]:::error
    end

    DB[(Database)]

    %% Relationships
    HTTP --> QBR
    QBR --> QB
    Config --> QB
    QB --> LEB
    
    QB --> F
    QB --> S
    QB --> I
    QB --> FS
    
    F --> FQ
    FQ --> FT
    FT --> FO
    F --> AF
    
    S --> SQ
    SQ --> ST
    ST --> SD
    S --> AS
    
    I --> IQ
    IQ --> IT
    IT --> II
    I --> AI
    
    FS --> FQ2
    
    F --> DB
    S --> DB
    I --> DB
    FS --> DB
    
    SP --> QB
    QB --> EH

    %% Styles
    classDef core fill:#3498db,stroke:#2980b9,color:white
    classDef feature fill:#2ecc71,stroke:#27ae60,color:white
    classDef config fill:#f1c40f,stroke:#f39c12,color:black
    classDef support fill:#e67e22,stroke:#d35400,color:white
    classDef error fill:#e74c3c,stroke:#c0392b,color:white

    %% Click Events
    click QB "https://github.com/spatie/laravel-query-builder/blob/main/src/QueryBuilder.php"
    click QBR "https://github.com/spatie/laravel-query-builder/blob/main/src/QueryBuilderRequest.php"
    click SP "https://github.com/spatie/laravel-query-builder/blob/main/src/QueryBuilderServiceProvider.php"
    click FQ "https://github.com/spatie/laravel-query-builder/blob/main/src/Concerns/FiltersQuery.php"
    click FT "https://github.com/spatie/laravel-query-builder/tree/main/src/Filters/"
    click FO "https://github.com/spatie/laravel-query-builder/blob/main/src/Enums/FilterOperator.php"
    click SQ "https://github.com/spatie/laravel-query-builder/blob/main/src/Concerns/SortsQuery.php"
    click ST "https://github.com/spatie/laravel-query-builder/tree/main/src/Sorts/"
    click SD "https://github.com/spatie/laravel-query-builder/blob/main/src/Enums/SortDirection.php"
    click IQ "https://github.com/spatie/laravel-query-builder/blob/main/src/Concerns/AddsIncludesToQuery.php"
    click IT "https://github.com/spatie/laravel-query-builder/tree/main/src/Includes/"
    click II "https://github.com/spatie/laravel-query-builder/blob/main/src/Includes/IncludeInterface.php"
    click FQ2 "https://github.com/spatie/laravel-query-builder/blob/main/src/Concerns/AddsFieldsToQuery.php"
    click EH "https://github.com/spatie/laravel-query-builder/tree/main/src/Exceptions/"
    click Config "https://github.com/spatie/laravel-query-builder/blob/main/config/query-builder.php"
    click AF "https://github.com/spatie/laravel-query-builder/blob/main/src/AllowedFilter.php"
    click AI "https://github.com/spatie/laravel-query-builder/blob/main/src/AllowedInclude.php"
    click AS "https://github.com/spatie/laravel-query-builder/blob/main/src/AllowedSort.php"
```
