Download the PHP package spatie/laravel-stats without Composer
On this page you can find all versions of the php package spatie/laravel-stats. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download spatie/laravel-stats
More information about spatie/laravel-stats
Files in spatie/laravel-stats
Package laravel-stats
Short Description Track application stat changes over time
License MIT
Homepage https://github.com/spatie/laravel-stats
Informations about the package laravel-stats
Track application stat changes over time
This package is a lightweight solution to summarize changes in your database over time. Here's a quick example where we are going to track the number of subscriptions and cancellations over time.
First, you should create a stats class.
Next, you can call increase
on it when somebody subscribes, and decrease
when somebody cancels their plan.
With this in place, you can query the stats. Here's how you can get the subscription stats for the past two months, grouped by week.
This will return an array like this one:
Support us
We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.
Installation
You can install the package via composer:
You must publish and run the migrations with:
Usage
Step 1: create a stats class
First, you should create a stats class. This class is responsible for configuration how a particular statistic is stored. By default, it needs no configuration at all.
By default, the name of the class will be used to store the statistics in the database. To customize the used key, use getName
Step 2: call increase and decrease or set a fixed value
Next, you can call increase
, decrease
when the stat should change. In this particular case, you should call increase
on it when somebody subscribes, and decrease
when somebody cancels their plan.
Instead of manually increasing and decreasing the stat, you can directly set it. This is useful when your particular stat does not get calculated by your own app, but lives elsewhere. Using the subscription example, let's image that subscriptions live elsewhere, and that there's an API call to get the count.
By default, that increase
, decrease
and set
methods assume that the event that caused your stats to change, happened right now. Optionally, you can pass a date time as a second parameter to these methods. Your stat change will be recorded as if it happened on that moment.
Step 3: query the stats
With this in place, you can query the stats. You can fetch stats for a certain period and group them by minute, hour, day, week, month, or year.
Here's how you can get the subscription stats for the past two months, grouped by week.
This will return an array containing arrayable Spatie\Stats\DataPoint
objects. These objects can be cast to arrays like this:
Extended Use-Cases
Read and Write from a custom Model
- Create a new table with
type (string)
,value (bigInt)
,created_at
,updated_at
fields - Create a model and add
HasStats
-trait
Read and Write from a HasMany-Relationship
Testing
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
- Alex Vanderbist
- Freek Van der Herten
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
All versions of laravel-stats with dependencies
illuminate/contracts Version ^8.73|^9.0|^10.0|^11.0
spatie/laravel-package-tools Version ^1.9.2