- How do I install and configure romanzipp/laravel-projectable-aggregates in a Laravel app?
- Install it with composer require romanzipp/laravel-projectable-aggregates, then publish the package config and migrations and run migrate. Add aggregate columns to the parent model, apply the Projectable trait, and define projectors that declare each count, sum, average, min, or max. The evaluation describes this as an afternoon-scale mechanical install; the harder work is choosing which relations to project and backfilling safely.
- Which Laravel and PHP versions does laravel-projectable-aggregates support?
- The provided context does not include a verified PHP/Laravel compatibility matrix or CI test results, so check the repository composer.json and release history before adopting. The package is aimed at conventional Laravel applications with Eloquent models, and the evaluation flags possible Laravel-version lag because it is low-adoption and single-maintainer. Pin the version you validate and plan a fork-on-break contingency.
- How do I define count, sum, average, min, and max projections with this package?
- You create a projector class that extends the package's Projector base class and declaratively list the aggregates for a relation, such as Count, Sum, Avg, Min, or Max. The parent model uses the Projectable trait and has matching denormalized columns for those values. The evaluation notes multiple aggregates per relation is a key question to verify in the source.
- Does laravel-projectable-aggregates automatically keep denormalized aggregate columns in sync?
- Yes, the package is designed to keep projected aggregate values in sync from related Eloquent models onto the parent model, avoiding repeated withCount and withSum-style queries. However, synchronization depends on writes going through Eloquent events. Any path that bypasses Eloquent can leave the stored aggregate stale, so you need reconciliation and monitoring.
- What happens to projected aggregates if I use bulk inserts, upserts, raw SQL, or external services?
- Those write paths can skip Eloquent events and silently desynchronize the stored aggregate values. The evaluation calls staleness and drift the defining risk of this pattern, not just a technical detail. Use a documented backfill or recompute command and a nightly reconciliation job that detects and repairs deltas.
- Is there an Artisan command to backfill or recompute projected aggregate values?
- The evaluation references running a recompute command as part of installation and backfill, so the package is expected to provide an explicit recompute or backfill Artisan command. Verify the exact command name and behavior in the source. For large tables, run backfills in chunks with chunkById and throttle them to avoid lock escalation.
- Are aggregate increments atomic and safe under concurrent writes in laravel-projectable-aggregates?
- The provided evaluation does not verify whether the implementation uses atomic increment and decrement operations or read-modify-write updates. This matters on hot parent rows because non-atomic counters invite row-lock contention and lost updates. Audit the projector source before using it for high-write models, and load-test concurrency if accuracy is important.
- Does laravel-projectable-aggregates handle soft deletes and all Eloquent model events?
- The context flags this as an open question: you should verify which events trigger recompute, including created, updated, deleted, restored, and forceDeleted, and whether soft deletes are handled. It also does not confirm whether recompute is synchronous, queued, or both. Read the projector source and test restore and force-delete scenarios before relying on the stored values.
- What are the main risks of using projected aggregates instead of withCount or withSum in Laravel?
- The main risks are drift from writes that bypass Eloquent events, concurrency and lost updates on hot parent rows, and unclear transactional semantics when a source write and projection update fail separately. There is also maintainer concentration: the evaluation notes 13 stars, zero dependents, and a single maintainer. Use it where read-heavy endpoints need persisted aggregates, not where exact real-time accuracy is contractual or write throughput is already the bottleneck.
- How should I test and monitor drift after adopting laravel-projectable-aggregates in production?
- Spike on one low-risk model such as Post.comments_count behind a flag, keep query-based reads live, and dual-compute both values while logging mismatches for one to two weeks. Backfill with the recompute command or a chunked job, then cut reads over endpoint by endpoint with a fallback. Stand up a nightly reconciliation job that recomputes aggregates and reports or repairs deltas, which the evaluation calls non-negotiable.