PHPackages                             openplain/laravel-flowfield - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. openplain/laravel-flowfield

ActiveLibrary[Database &amp; ORM](/categories/database)

openplain/laravel-flowfield
===========================

Cache-backed computed aggregate fields for Eloquent — inspired by Navision's FlowField concept.

v1.0.0(3mo ago)1591[1 PRs](https://github.com/openplain/laravel-flowfield/pulls)MITPHPPHP ^8.1

Since Mar 17Pushed 3mo agoCompare

[ Source](https://github.com/openplain/laravel-flowfield)[ Packagist](https://packagist.org/packages/openplain/laravel-flowfield)[ Docs](https://github.com/openplain/laravel-flowfield)[ RSS](/packages/openplain-laravel-flowfield/feed)WikiDiscussions main Synced 3w ago

READMEChangelogDependencies (5)Versions (2)Used By (0)

Laravel FlowField
=================

[](#laravel-flowfield)

[![Latest Version on Packagist](https://camo.githubusercontent.com/66c95b9aedac115ca4dcabfbbcea8049380b4eae6032a1baf5c43fe590623bd3/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6f70656e706c61696e2f6c61726176656c2d666c6f776669656c642e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/openplain/laravel-flowfield)[![Total Downloads](https://camo.githubusercontent.com/262bb51c7c462f2ac90aa8aa2d1023d70679deb3b987b034b2cf47390e35aa49/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6f70656e706c61696e2f6c61726176656c2d666c6f776669656c642e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/openplain/laravel-flowfield)

Cache-backed computed aggregate fields for Eloquent — inspired by Navision's FlowField concept.

Why This Package?
-----------------

[](#why-this-package)

When your `Customer` model needs to show a balance (sum of all ledger entries), or your `Item` needs `inventory_quantity` (sum of stock movements), you have two bad options: run the aggregate query every time (slow with thousands of entries), or store a denormalized total and keep it in sync manually (fragile — things drift, you build a "recalc" button).

In the late 1980s, three Danish engineers at PC&amp;C (later Navision, now Microsoft Business Central) solved this exact problem. Their answer was **FlowFields** — virtual fields that compute aggregates on demand without storing the result in the database. Navision defined seven FlowField types: Sum, Count, Average, Min, Max, Exist, and Lookup. For Sum fields specifically, they built **SIFT** (Sum Index Field Technology) — pre-calculated indexes maintained on every write to make sum lookups instant. This concept has powered millions of ERP installations for over 35 years.

We brought the FlowField concept to Laravel. Where Navision uses SIFT indexes for sums and live queries for the rest, we use your cache layer (Redis/Memcached) as the performance layer for all aggregate types.

**Our Goal:** Declare aggregate fields as model attributes. Computed once, cached in Redis/Memcached, automatically invalidated when data changes. Instant reads. Zero maintenance. No stale data.

### Built on Proven Technology

[](#built-on-proven-technology)

- **Laravel Cache** - Uses your existing in-memory cache (Redis or Memcached) for instant lookups
- **PHP 8.1 Attributes** - Clean, declarative syntax for defining computed fields
- **Eloquent Events** - Automatic cache invalidation via model observers

Features
--------

[](#features)

- ⚡ **Instant Reads** - Aggregate values served from cache, not computed on every request
- 🔄 **Auto-Invalidation** - Cache busts automatically when related records change
- 🎯 **Declarative Syntax** - Define FlowFields with PHP attributes, no boilerplate
- 📊 **All Aggregates** - Supports `sum`, `count`, `avg`, `min`, `max`, and `exists`
- 🔧 **Redis &amp; Memcached** - Designed for in-memory cache stores for true instant reads
- 🛡️ **Fault Tolerant** - Falls back to live queries if cache is unavailable
- 📦 **Zero Schema Changes** - No migrations, no database modifications
- 🔑 **Smart Invalidation** - Only invalidates when relevant columns change

Requirements
------------

[](#requirements)

- PHP 8.1 or higher
- Laravel 10, 11, or 12

Installation
------------

[](#installation)

Install via Composer:

```
composer require openplain/laravel-flowfield
```

Optionally publish the configuration file:

```
php artisan vendor:publish --tag=flowfield-config
```

Quick Start
-----------

[](#quick-start)

### 1. Define FlowFields on Your Parent Model

[](#1-define-flowfields-on-your-parent-model)

Add the `HasFlowFields` trait and use the `#[FlowField]` attribute on accessor methods:

```
