PHPackages                             justinbarkhuff/laravel-stats - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. justinbarkhuff/laravel-stats

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

justinbarkhuff/laravel-stats
============================

A fork of spatie/laravel-stats which is backwards compatible with PHP 7.4+ and MySQL 5.7

v1.1(5y ago)1861MITPHPPHP ^7.4

Since Mar 29Pushed 5y agoCompare

[ Source](https://github.com/justinbarkhuff/laravel-stats)[ Packagist](https://packagist.org/packages/justinbarkhuff/laravel-stats)[ Docs](https://github.com/justinbarkhuff/laravel-stats)[ GitHub Sponsors](https://github.com/sponsors/spatie)[ Fund](https://spatie.be/open-source/support-us)[ RSS](/packages/justinbarkhuff-laravel-stats/feed)WikiDiscussions master Synced today

READMEChangelog (1)Dependencies (6)Versions (4)Used By (0)

Track application stat changes over time
========================================

[](#track-application-stat-changes-over-time)

[![Latest Version on Packagist](https://camo.githubusercontent.com/76f19fb6848bbc3ce9dbf6a43c55eb9251530940a93c41aa22cec9995d6bad0b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6a757374696e6261726b687566662f6c61726176656c2d73746174732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/justinbarkhuff/laravel-stats)[![Tests](https://github.com/justinbarkhuff/laravel-stats/actions/workflows/run-tests.yml/badge.svg)](https://github.com/justinbarkhuff/laravel-stats/actions/workflows/run-tests.yml)[![Total Downloads](https://camo.githubusercontent.com/51508310d56482b606e259eb74f4a6b0cd72b90a0a6c49e416c5fe9b51cb9ec6/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6a757374696e6261726b687566662f6c61726176656c2d73746174732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/justinbarkhuff/laravel-stats)

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.

```
use Spatie\Stats\BaseStats;

class SubscriptionStats extends BaseStats {}
```

Next, you can call `increase` on it when somebody subscribes, and `decrease` when somebody cancels their plan.

```
SubscriptionStats::increase(); // execute whenever somebody subscribes
SubscriptionStats::decrease() // execute whenever somebody cancels the subscription;
```

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.

```
use Spatie\Stats\StatsQuery;

$stats = StatsQuery::for(SubscriptionStats::class)
    ->start(now()->subMonths(2))
    ->end(now()->subSecond())
    ->groupByWeek()
    ->get();
```

This will return an array like this one:

```
[
    [
        'start' => '2020-01-01',
        'end' => '2020-01-08',
        'value' => 102,
        'increments' => 32,
        'decrements' => 20,
        'difference' => 12,
    ],
    [
        'start' => '2020-01-08',
        'end' => '2020-01-15',
        'value' => 114,
        'increments' => 63,
        'decrements' => 30,
        'difference' => 33,
    ],
]
```

Support us
----------

[](#support-us)

[![](https://camo.githubusercontent.com/eec8acc6cd7edcee3f91bc238f1a4e9188ff97a1c56b0bccc2a907a401220a85/68747470733a2f2f6769746875622d6164732e73332e65752d63656e7472616c2d312e616d617a6f6e6177732e636f6d2f6c61726176656c2d73746174732e6a70673f743d32)](https://spatie.be/github-ad-click/laravel-stats)

We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).

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](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).

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

[](#installation)

You can install the package via composer:

```
composer require justinbarkhuff/laravel-stats
```

You must publish and run the migrations with:

```
php artisan vendor:publish --provider="Spatie\Stats\StatsServiceProvider" --tag="stats-migrations"
php artisan migrate
```

Usage
-----

[](#usage)

### Step 1: create a stats class

[](#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.

```
use Spatie\Stats\BaseStats;

class SubscriptionStats extends BaseStats {}
```

By default, the name of the class will be used to store the statistics in the database. To customize the used key, use `getName`

```
use Spatie\Stats\BaseStats;

class SubscriptionStats extends BaseStats
{
    public function getName() : string{
        return 'my-custom-name'; // stats will be stored with using name `my-custom-name`
    }
}
```

Step 2: call increase and decrease or set a fixed value
-------------------------------------------------------

[](#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.

```
SubscriptionStats::increase(); // execute whenever somebody subscribes
SubscriptionStats::decrease(); // execute whenever somebody cancels the subscription;
```

Instead of manually increasing and decreasing the stat, you can directly set it. This is useful when you 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.

```
$count = AnAPi::getSubscriptionCount();

SubscriptionStats::set($count);
```

By default, that `increase`, `decrease` and `sets` 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.

```
SubscriptionStats::increase(1, $subscription->created_at);
```

### Step 3: query the stats

[](#step-3-query-the-stats)

With this in place, you can query the stats. You can fetch stats for a certain period and group it by day, week, month.

Here's how you can get the subscription stats for the past two months, grouped by week.

```
$stats = SubscriptionStats::query()
    ->start(now()->subMonths(2))
    ->end(now()->subSecond())
    ->groupByWeek()
    ->get();
```

This will return an array containing arrayable `Spatie\Stats\DataPoint` objects. These objects can be cast to arrays like this:

```
// output of $stats->toArray():
[
    [
        'start' => '2020-01-01',
        'end' => '2020-01-08',
        'value' => 102,
        'increments' => 32,
        'decrements' => 20,
        'difference' => 12,
    ],
    [
        'start' => '2020-01-08',
        'end' => '2020-01-15',
        'value' => 114,
        'increments' => 63,
        'decrements' => 30,
        'difference' => 33,
    ],
]
```

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [Alex Vanderbist](https://github.com/AlexVanderbist)
- [Freek Van der Herten](https://github.com/freekmurze)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

27

—

LowBetter than 47% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity55

Maturing project, gaining track record

 Bus Factor2

2 contributors hold 50%+ of commits

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~12 days

Total

3

Last Release

1897d ago

Major Versions

0.0.1 → 1.0.02021-04-14

PHP version history (2 changes)0.0.1PHP ^8.0

v1.1PHP ^7.4

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/868240?v=4)[Justin Barkhuff](/maintainers/justinbarkhuff)[@justinbarkhuff](https://github.com/justinbarkhuff)

---

Top Contributors

[![freekmurze](https://avatars.githubusercontent.com/u/483853?v=4)](https://github.com/freekmurze "freekmurze (36 commits)")[![AlexVanderbist](https://avatars.githubusercontent.com/u/6287961?v=4)](https://github.com/AlexVanderbist "AlexVanderbist (30 commits)")[![justinbarkhuff](https://avatars.githubusercontent.com/u/868240?v=4)](https://github.com/justinbarkhuff "justinbarkhuff (8 commits)")[![abenerd](https://avatars.githubusercontent.com/u/7523903?v=4)](https://github.com/abenerd "abenerd (1 commits)")[![SachinGanesh](https://avatars.githubusercontent.com/u/11994640?v=4)](https://github.com/SachinGanesh "SachinGanesh (1 commits)")

---

Tags

spatielaravel-stats

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/justinbarkhuff-laravel-stats/health.svg)

```
[![Health](https://phpackages.com/badges/justinbarkhuff-laravel-stats/health.svg)](https://phpackages.com/packages/justinbarkhuff-laravel-stats)
```

###  Alternatives

[spatie/laravel-permission

Permission handling for Laravel 12 and up

12.9k102.4M1.4k](/packages/spatie-laravel-permission)[spatie/laravel-pdf

Create PDFs in Laravel apps

1.0k4.8M47](/packages/spatie-laravel-pdf)[spatie/laravel-data

Create unified resources and data transfer objects

1.8k35.2M967](/packages/spatie-laravel-data)[codewithdennis/filament-select-tree

The multi-level select field enables you to make single selections from a predefined list of options that are organized into multiple levels or depths.

329530.5k29](/packages/codewithdennis-filament-select-tree)[spatie/laravel-passkeys

Use passkeys in your Laravel app

471890.7k39](/packages/spatie-laravel-passkeys)[spatie/laravel-stats

Track application stat changes over time

452292.0k7](/packages/spatie-laravel-stats)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
