Laravel Time Series provides an API to create and maintain projected data from you Eloquent models, and represent them as time-series.
timothepearce/laravel-time-series is a Laravel package for laravel time series provides an api to create and maintain projected data from you eloquent models, and represent them as time-series..
It currently has 96 GitHub stars and 5.363 downloads on Packagist (latest version v1.0.0-beta-9).
Install it with composer require timothepearce/laravel-time-series.
Discover more Laravel packages by timothepearce
or browse all Laravel packages to compare alternatives.
Last updated
Build your time series with ease
Laravel Time Series provides an API to projects data from your Eloquent models, and convert them to time series.
The full documentation can be found here.
composer require timothepearce/laravel-time-series
php artisan migrate
php artisan make:projection MyProjection
When you want to make your model projectable, you must add it the Projectable trait and define the $projections class attribute:
use App\Models\Projections\MyProjection;
use TimothePearce\TimeSeries\Projectable;
class MyProjectableModel extends Model
{
use Projectable;
protected array $projections = [
MyProjection::class,
];
}
If you want to use a different date field from your Model instead of created_at then do the following :
use App\Models\Projections\MyProjection;
use TimothePearce\TimeSeries\Projectable;
class MyProjectableModel extends Model
{
use Projectable;
protected $casts = [
'other_date_time' => 'datetime:Y-m-d H:00',
];
protected array $projections = [
MyProjection::class,
];
}
namespace App\Models\Projections;
use Illuminate\Database\Eloquent\Model;
use TimothePearce\TimeSeries\Contracts\ProjectionContract;
use TimothePearce\TimeSeries\Models\Projection;
class MyProjection extends Projection implements ProjectionContract
{
/**
* The projected periods.
*/
public array $periods = [];
public string $dateColumn = 'other_date_time';
....
When you're implementing a projection, follow theses three steps:
A Projection is an Eloquent model and is queried the same way, but keep in mind that the projections are all stored in a single table. That means you'll have to use scope methods to get the correct projections regarding the period you defined earlier:
MyProjection::period('1 day')
->between(
today()->subDay(), // start date
today(), // end date
)
->get();
To get a time series from a projection model, use the toTimeSeries method:
MyProjection::period('1 day')
->toTimeSeries(
today()->subDay(),
today(),
);
Note that this method fill the missing projections between the given dates with the default content you defined earlier.
The MIT License (MIT). Please see License File for more information.