Download the PHP package mattkingshott/statistics without Composer
On this page you can find all versions of the php package mattkingshott/statistics. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mattkingshott/statistics
More information about mattkingshott/statistics
Files in mattkingshott/statistics
Package statistics
Short Description A package to maintain statistics on database records
License MIT
Homepage https://github.com/mattkingshott/statistics
Informations about the package statistics
Statistics
This package enables a Laravel application to maintain aggregated statistics for database tables. It serves as a companion package to (and relies upon) triggers.
Who is this for?
If you're running queries that are slow because they need to perform aggregations (COUNT
, SUM
, MIN
, MAX
or AVG
) across many records, then you might get some value from this package. A common scenario where this takes place, is on a dashboard that displays lots of statistics e.g.
By contrast, you can configure the package to automatically maintain statistics in the background. So, instead of a slow query (like the above example), you can instead do this:
Or, better still, use the Eloquent model to query the data:
Table | Values |
---|---|
Articles | { "count" : 6 } |
Projects | { "count" : 3 } |
Tasks | { "count" : 2 } |
Installation
Pull in the package using Composer:
Configuration
The package includes a configuration file that allows you to change the name of the database table that contains the aggregated values (the default is 'statistics'). If you want to change it, publish the configuration file using Artisan:
Usage
The package will automatically register and migrate a statistics
table to your database. This table then serves as a repository for aggregated values. The values are maintained using database triggers, which will automatically fire after a record is inserted, updated or deleted.
Before proceeding further, it is important to remember that database triggers (which the package relies on) can only be added to a table after it has been created. In other words, don't try to create statistics for a table before it has been created by
Schema::create
(this will become clearer in the examples below).
To begin, add the InteractsWithStatistics
trait to any Model
class that you want to maintain statistics for e.g.
Next, call the static track
method on the Model
.
Next, call one or more of the available aggregation methods:
You can call an aggregation method more than once if you need to maintain statistics on multiple columns. Simply supply a custom name to differentiate them:
Finally, call the create
method to install the triggers.
Example
Here's a simple example within a database migration:
Contributing
Thank you for considering a contribution to the package. You are welcome to submit a PR containing improvements, however if they are substantial in nature, please also be sure to include a test or tests.
License
The MIT License (MIT). Please see License File for more information.