Download the PHP package romanzipp/laravel-projectable-aggregates without Composer
On this page you can find all versions of the php package romanzipp/laravel-projectable-aggregates. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download romanzipp/laravel-projectable-aggregates
More information about romanzipp/laravel-projectable-aggregates
Files in romanzipp/laravel-projectable-aggregates
Package laravel-projectable-aggregates
Short Description Projectable aggregates for Laravel
License MIT
Informations about the package laravel-projectable-aggregates
Laravel Projectable Aggregates
What
Laravel Projectable Aggregates is a package that allows you to easily store aggregate values like counts, sums, averages, etc. in your models eliminating the need to calculate these values on the fly (with withCount
, withSum
, withAvg
, etc.).
- Speed up database queries by storing aggregate values in the database.
- Automatically updates aggregate values with Model Events.
- Option to calculate the aggregate values periodically in bulk.
Installation
Terminology
🟢 Consumers
Consumers hold the projectable aggregate database field. This is the model which otherwise would calculate the relationship fields via withCount
, withSum
, withAvg
, etc.
🔵 Providers
Providing models provide (duh) the aggregate values for the consumer. Think of the provider to exist many times for one consumer.
Usage
Let's continue with the example of a Car
model with Door
models. We want to store the Doors count in the Car's project_doors_count
field.
1. Add a Projection Field to DB
2. Update your Models
🟢 Car (Consumer)
The consumer model will attach the ConsumesProjectableAggregate
attribute to the provider relation.
🔵 Door (Provider)
The provider model will attach the ProvidesProjectableAggregate
attribute to the consumer relation.
3. Register the Projection Aggregates
In order to listen to model events issued by the provider models, you need to register the consumer models in the boot
method of your AppServiceProvider
.
Documentation
[!IMPORTANT]
Calculating aggregate values (without bulk) relies on Elouent model events which are only dispatched when working with Eloquent model itself. Using theDB
facade will not trigger the library to update the aggregate values.
Aggregate Types
There are three types of aggregates that can be calculated:
ProjectionAggregateType::TYPE_COUNT
: Counts the number of related models.ProjectionAggregateType::TYPE_SUM
: Sums the related models' values.ProjectionAggregateType::TYPE_AVG
: Averages the related models' values.
[!IMPORTANT]
In order to use the aggregate typesTYPE_SUM
andTYPE_AVG
, you need to specify the target attribute of the relationship.
Triggers
You can decide if you would only like to rely on models events or if you want to calculate the aggregate values periodically in bulk.
Rely on Model Events
This will automatically work if your've attached the ProvidesProjectableAggregate
attribute to your provider relations. Once a provider model has been created/deleted the according consumer aggregate attribute will be incremented/decremented.
Calculate periodically in Bulk
If you don't want to or can't rely on model events, you can use the bulk-aggregate
command to calculate the aggregate values periodically in bulk.
--queued
: Dispatch a job to the worker queue.--queue=
: Specify the queue to run the command in.--class=
: Limit the bulk calculation to a specific consumer class.
Relationships
The following relationships are supported and tested:
Consumer::hasMany()
- ✅ Model Events
- ✅ Bulk Aggregation
Consumer::hasManyThrough()
⚠️ WIP
- ❌ Model Events
- ✅ Bulk Aggregation
Consumer::morphMany()
- ✅ Model Events
- ✅ Bulk Aggregation
Testing
This repository contains a Lando configuration file that can be used to run the tests on your local machine.
License
The MIT License (MIT). Please see License File for more information.