PHP

Queuing up in meetings

Distributing speaking time can be tricky when meeting face to face, but it is usuallly worse in virtual meetings. Especially those spanning long distances. In my current team, I learned how queues in remote meetings Read more…

By , ago
PHP

★ Injecting extra data in the payload of queued jobs in Laravel

[AdSense-A]

I'm proud to announce yet another package by our team: spatie/laravel-interacts-with-payload. This one, which was inspired by a blog post by James Brooks, can inject data in all your jobs with just one line of code.

In this video, you'll see the package in action, I explain the internals and the tests. If you prefer reading, continue below the video.

How you can inject things into every job of your app

Imagine that you want to have the user who initiated the queued job available in every queued job. Also, assume that your app has tens or hundreds of jobs you don't want to update manually.

Using this package, this is how you would solve that. Just use AllJobs::add to add things to all jobs.

// typically in the boot method of a service provider

use Spatie<span class="hljs-title">InteractsWithPayload<span class="hljs-title">Facades<span class="hljs-title">AllJobs;

AllJobs::add('user', fn() => auth()->user());

To retrieve the user in your job, you can call getFromPayload, which is available through the InteractsWithPayload trait.

use Illuminate<span class="hljs-title">Contracts<span class="hljs-title">Queue<span class="hljs-title">ShouldQueue;
use Spatie<span class="hljs-title">InteractsWithPayload<span class="hljs-title">Concerns<span class="hljs-title">InteractsWithPayload;

class YourJob implements ShouldQueue { use InteractsWithPayload;

<span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">handle</span><span class="hljs-params">()</span>
</span>{
    <span class="hljs-comment">// instance of User model or `null`</span>
    $user = <span class="hljs-keyword">$this</span>-&gt;getFromPayload(<span class="hljs-string">'user'</span>);
}  

}

If you want to know how the internals of this package work, watch the video linked above or read the code in the repo on GitHub.

(more…)

By , ago
PHP

Self hosting Google Fonts

To self-host, you need to download the fonts and write the appropriate @font-face CSS rules. Instead of doing this myself, Seb came across an excellent tool to streamline this process Read More

By , ago
PHP

★ A lightweight Laravel package to track changes over time

[AdSense-A]

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 SpatieStatsBaseStats;

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<span class="hljs-title">Stats<span class="hljs-title">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.

(more…)

By , ago