Mastering Nova is the first comprehensive, end-to-end, course that is launched by Bruno Falcao that covers all the aspects of Laravel Nova. The course was structured for anyone that wants to learn Nova, from beginners to advanced level.

A lightweight Laravel package to track changes over time

Original – by Alex Vanderbist and Freek Van der Herten – 3 minute read

I'm proud to announce that our team has released a new package called spatie/laravel-stats. This package is a lightweight solution for summarizing changes in your database over time.

Tracking changes

Here's a quick example where we will 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 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,
    ],
]

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 imagine 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 at that moment.

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

How it works on under the hood

The implementation of this package is simple. The basic principles of event sourcing are being used: we don't store a result, but only the changes.

The package stores all "events" in the stats_event table

screenshot

Inside the StatsQuery class, you'll find the heart of the package. In its get function, you can see that all events for a given period are retrieved, summarized and mapped to DataPoint classes.

In closing

We are going to use laravel-stats in Flare, our exception tracker for Laravel/PHP/JavaScript projects, to keep tracker of changes in subscribers and other key metrics. We hope that the package can be helpful in your projects as well.

As mentioned above, the package uses a lightweight event sourcy approach. If you want to know more about event sourcing, check out our upcoming premium course on event sourcing in Laravel.

I'd like to thank my colleague Alex, who did the bulk of the work creating spatie/laravel-stats.

Do also take a look at this list of packages our team has created previously. I'm sure there's something there for your next project.

Stay up to date with all things Laravel, PHP, and JavaScript.

Follow me on Twitter. I regularly tweet out programming tips, and what I myself have learned in ongoing projects.

Every two weeks I send out a newsletter containing lots of interesting stuff for the modern PHP developer.

Expect quick tips & tricks, interesting tutorials, opinions and packages. Because I work with Laravel every day there is an emphasis on that framework.

Rest assured that I will only use your email address to send you the newsletter and will not use it for any other purposes.

Comments

Webmentions

Qmonitor.io liked on 19th April 2021
Meet Thosar liked on 17th April 2021
Piotr Gumienny liked on 15th April 2021
Shafqat ali liked on 15th April 2021
Phil Rowley liked on 15th April 2021
Lee Overy liked on 15th April 2021
Ruslan liked on 15th April 2021
Zysk Technologies liked on 15th April 2021
Boris DEHOUMON liked on 15th April 2021
Pushpak liked on 15th April 2021
Burhan liked on 15th April 2021
José Cage liked on 15th April 2021
Mohamed Boudelaa liked on 15th April 2021
Karol Krakowiak liked on 14th April 2021
Kory Gorsky liked on 14th April 2021
Guus liked on 14th April 2021
Antonio Garcia liked on 14th April 2021
Justas Raudonius retweeted on 14th April 2021
Spatie retweeted on 14th April 2021
Martin Medina liked on 14th April 2021
Justas Raudonius liked on 14th April 2021
π liked on 14th April 2021
Sonicastra liked on 14th April 2021
🔴 ¬ Bremse 🔴 liked on 14th April 2021
Parthasarathi G K liked on 14th April 2021
Already see one use case for my current project 🙂 thanks you guys
bigboi69420 liked on 14th April 2021
bernard kssy 💻 Busy retweeted on 14th April 2021
فرودو liked on 14th April 2021
Felipe Vega 👨🏻‍💻 liked on 14th April 2021
Nathanael McDaniel 🖥 liked on 14th April 2021
Martin Dufresne liked on 14th April 2021
Jigal Sanders liked on 14th April 2021
Goran Galinec liked on 14th April 2021
Neil Carlo Faisan Sucuangco liked on 14th April 2021
Afaz Khatri liked on 14th April 2021
Jr 👨‍💻 liked on 14th April 2021
Quinn liked on 14th April 2021
ダビッド トレス retweeted on 14th April 2021
Lars Wiegers retweeted on 14th April 2021
Stefan Zweifel 🌿 liked on 14th April 2021
K. liked on 14th April 2021
ダビッド トレス liked on 14th April 2021
Tauseef shah liked on 14th April 2021
Julio Bitencourt liked on 14th April 2021
_D_ liked on 14th April 2021
Dixens liked on 14th April 2021
gary liked on 14th April 2021
airios liked on 14th April 2021
Asif Mulla liked on 14th April 2021
Vaggelis Yfantis liked on 14th April 2021
Bruno liked on 14th April 2021
Wyatt liked on 14th April 2021
بی‌صدا liked on 14th April 2021
Maciej Czechowski liked on 14th April 2021
Robin Dirksen liked on 14th April 2021