Laravel Model Caching

Automatic, self-invalidating query caching for Eloquent models with a single trait.

Library
Composer
v13.1.7
2,371stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
79/100Good
Development Activity72
Maintenance80
Community64
Maturity60
Momentum40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
83/100Excellent
Architecture84
Code Quality85
Innovation80
Learning Curve82

Laravel Model Caching makes Eloquent model caching effortless by adding automatic, self-invalidating query caching to your models. By applying a single Cachable trait, every query a model runs is transparently cached and re-served from your configured cache store until the underlying data changes.

The package hooks into Eloquent’s query builder and relationship layer to generate deterministic cache keys and tags, then flushes the relevant tags automatically whenever a model is created, updated, or deleted. This removes the boilerplate and correctness risks of hand-rolled caching while dramatically reducing database load on read-heavy applications.

What You Get

  • A Cachable trait that enables automatic query caching on any Eloquent model
  • Deterministic cache-key and cache-tag generation for models and relationships
  • Automatic cache invalidation on model create, update, and delete
  • Support for cached relationships including belongs-to-many and has-many-through
  • Artisan commands and options to flush or disable caching per model or query

Common Use Cases

  • Reducing database load on read-heavy Laravel applications
  • Caching expensive Eloquent relationship queries without manual key handling
  • Speeding up frequently accessed reference data that changes infrequently

Under The Hood

Architecture - The Cachable trait swaps Eloquent’s default builder for a CachedBuilder that wraps query execution in a cache lookup. CacheKey hashes the query, bindings, eager loads, and model class into a stable key, while CacheTags derives tag names so CachedModel observers can flush precisely the affected entries on write. Cached relationship classes (CachedBelongsToMany, CachedHasManyThrough, etc.) extend the same mechanism across associations.

Tech Stack - Pure PHP built on illuminate/database, illuminate/support, and illuminate/cache, supporting Laravel 11-13 and any tag-capable cache store such as Redis or Memcached. A service provider registers the console commands and model observers.

Code Quality - The repository advertises 335+ tests with Codecov coverage reporting and a GitHub Actions CI matrix across Laravel and PHP versions, indicating strong, well-maintained test discipline for a caching layer where correctness is critical.

API Design - Adoption is a single trait plus optional per-query escape hatches (disableCache(), cooldowns, flush commands). The zero-config default and drop-in trait keep the learning curve low while still exposing granular control when needed.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search