|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +Laravel package (`act-training/query-builder`) providing drop-in Livewire components for building query builders, tables, and reports on Eloquent models. Uses Spatie's `laravel-package-tools` for package scaffolding. |
| 8 | + |
| 9 | +Namespace: `ACTTraining\QueryBuilder` |
| 10 | + |
| 11 | +## Commands |
| 12 | + |
| 13 | +```bash |
| 14 | +composer test # Run tests (Pest) |
| 15 | +vendor/bin/pest --filter="test name" # Run a single test |
| 16 | +composer analyse # Static analysis (PHPStan, level 5) |
| 17 | +composer format # Code formatting (Laravel Pint) |
| 18 | +composer start # Build and serve via testbench |
| 19 | +``` |
| 20 | + |
| 21 | +## Architecture |
| 22 | + |
| 23 | +### Core Builder Classes (src/) |
| 24 | + |
| 25 | +Three abstract Livewire components that consuming apps extend: |
| 26 | + |
| 27 | +- **`QueryBuilder`** — Full-featured query builder with criteria-based filtering (AND/OR conditions). Users define `query()`, `columns()`, and `conditions()` methods. Renders `query-table` view. |
| 28 | +- **`TableBuilder`** — Simpler table with URL-persisted filters and search. Users define `query()`, `columns()`, and `filters()` methods. Renders `table` view. |
| 29 | +- **`ReportBuilder`** extends QueryBuilder — Adds report-specific features via `WithReportBuilder`. Renders `report-table` view. |
| 30 | +- **`GroupsBuilder`** extends QueryBuilder — Adds grouping via `WithGroupBuilder`. Renders `group-table` view. |
| 31 | + |
| 32 | +### Two Filtering Systems |
| 33 | + |
| 34 | +**Criteria System** (used by QueryBuilder): Conditions → Criteria → CriteriaCollection pipeline. |
| 35 | +- `Conditions` (src/Support/Conditions/) — Define available filter operations for the UI (TextCondition, DateCondition, BooleanCondition, etc.) |
| 36 | +- `Criteria` (src/Support/Criteria/) — Implement the actual query logic (CompareCriteria, LikeCriteria, DateCriteria, BooleanCriteria, NullCriteria). All extend `BaseCriteria` implementing `CriteriaInterface`. |
| 37 | +- `CriteriaCollection` (src/Support/Collection/) — Composes criteria with AND (`AllOfCriteriaCollection`) or OR (`OneOfCriteriaCollection`) logic. |
| 38 | +- Models must `use AppliesCriteria` trait to gain the `->apply()` scope. |
| 39 | + |
| 40 | +**Filter System** (used by TableBuilder): Simpler key/operator/value filters. |
| 41 | +- `Filters` (src/Support/Filters/) — TextFilter, DateFilter, BooleanFilter, NumberFilter, SelectFilter, NullFilter. All extend `BaseFilter`. |
| 42 | +- Filter codes use Blade-safe operator aliases (gte/lte/gt/lt instead of `>=`/`<=`/`>`/`<`) to avoid HTML encoding issues in `wire:model`. |
| 43 | + |
| 44 | +### Columns (src/Support/Columns/) |
| 45 | + |
| 46 | +Column types: `Column`, `BooleanColumn`, `DateColumn`, `ViewColumn`. All extend `BaseColumn`. Fluent API with `make()` static constructor. Support sorting, searching, hiding, custom components, justify alignment, subtitles, and value reformatting. |
| 47 | + |
| 48 | +### Behavior Traits (src/Support/Concerns/) |
| 49 | + |
| 50 | +All builder functionality is composed via traits: `WithActions`, `WithCaching`, `WithColumns`, `WithFilters`, `WithPagination`, `WithQueryBuilder`, `WithRowClick`, `WithRowInjection`, `WithSearch`, `WithSelecting`, `WithSorting`, `WithToolbar`, `WithViews`, `WithIdentifier`, `WithIndicator`, `WithMessage`. |
| 51 | + |
| 52 | +### Relationship Handling |
| 53 | + |
| 54 | +Dot-notation keys (e.g., `contract.job.name`) are used throughout columns, conditions, and filters to traverse Eloquent relationships. The pattern `explode('.', $key)` → `array_pop()` for column name → `implode('.')` for relationship path → `whereHas()` is used consistently across BaseCriteria, TableBuilder, and BaseColumn. |
| 55 | + |
| 56 | +## Testing |
| 57 | + |
| 58 | +Uses Pest with Orchestra Testbench. Tests are in `tests/`. The `TestCase` base class registers the service provider and configures factory resolution. Architecture tests enforce no debugging functions (`dd`, `dump`, `ray`). |
| 59 | + |
| 60 | +## Views |
| 61 | + |
| 62 | +Blade views in `resources/views/`. Four Blade components registered in the service provider for column rendering. View prefix is `query-builder::`. |
| 63 | + |
| 64 | +## Conventions |
| 65 | + |
| 66 | +- All public API classes use `static make($label, $key)` fluent constructors |
| 67 | +- `$key` defaults to `Str::snake($label)` when not provided |
| 68 | +- PHPStan level 5 with Octane compatibility and model property checks enabled |
| 69 | +- PHP 8.2+, Livewire v3 |
0 commit comments