Laravel Trend

repository·master·Indexed 22 days ago

https://github.com/flowframe/laravel-trend

A fluent package for generating time-based trends, charts, and reports from Eloquent models or custom queries. It supports various aggregate functions including count, sum, average, min, and max, and provides tools to configure time intervals, date columns, and time ranges.

Tokens
1.4K
Snippets
10
Records
11
Agent score
76%

What's inside laravel-trend

  1. Generate trends using Trend::model() and Trend::query()

    master

    To start a trend, you must use either Trend::model() or Trend::query().

    • Use Trend::model(Model::class) when you want to perform operations on the entire model.
    • Use Trend::query(Builder $query) when you need to apply additional Eloquent filters (like where, has, etc.) before calculating the trend.
    // Using model directly
    Trend::model(Order::class)
        ->between(...)
        ->perDay()
        ->count();
    
    // Using a specific query with filters
    Trend::query(
        Order::query()
            ->hasBeenPaid()
            ->hasBeenShipped()
    )
        ->between(...)
        ->perDay()
        ->count();
  2. Specify a custom date column

    master

    By default, the package assumes your model uses a created_at column. If your model uses a different column for dates, or if you want to aggregate based on a different timestamp, use the dateColumn(string $column) method.

    Trend::model(Order::class)
        ->dateColumn('custom_date_column')
        ->between(...)
        ->perDay()
        ->count();
  3. Calculate trend aggregates

    master

    Once the trend is configured, you can execute an aggregate function to retrieve a collection of TrendValue objects. The package automatically fills in gaps in the timeline with zero-value TrendValue objects to ensure a continuous series.

    Available aggregate methods:

    • count(string $column = '*'): Counts occurrences.
    • sum(string $column): Sums the values of a column.
    • average(string $column): Calculates the average of a column.
    • min(string $column): Finds the minimum value in a column.
    • max(string $column): Finds the maximum value in a column.
    • aggregate(string $column, string $aggregate): Allows for custom SQL aggregate functions.
    $results = Trend::model(Order::class)
        ->between('2023-01-01', '2023-01-31')
        ->perDay()
        ->sum('total_amount');
    
    // $results is a Collection of TrendValue objects
    // Each TrendValue has: 
    // - date: (string) formatted date
    // - aggregate: (mixed) the calculated value
  4. Configure the trend time range and interval

    master

    To generate a trend, you must define the time period and the granularity (interval) of the data points.

    • between($start, $end): Sets the start and end dates for the trend. Accepts Carbon instances or compatible date strings.
    • interval(string $interval): Sets a custom interval.
    • Convenience methods for intervals:
      • perMinute()
      • perHour()
      • perDay()
      • perWeek()
      • perMonth()
      • perYear()
    $trend->between('2023-01-01', '2023-01-31')
          ->perDay();
  5. Configure date columns and aliases

    master

    By default, the package uses created_at as the date column and date as the alias. You can override these if your table uses different naming conventions.

    • dateColumn(string $column): Specifies the column used for the time dimension.
    • dateAlias(string $alias): Specifies the alias used in the resulting collection for the date key.
    $trend->dateColumn('updated_at')
          ->dateAlias('timestamp');
  6. Initialize a trend with model() or query()

    master

    You can start generating trends using either a specific Eloquent model or an existing Eloquent query builder instance.

    • Use Trend::model(ModelName::class) to start a trend analysis from a model's base query.
    • Use Trend::query($builder) to start from a custom Eloquent query builder instance (e.g., after applying where or with clauses).
    // Using a model
    $trend = Trend::model(User::class);
    
    // Using a query builder
    $trend = Trend::query(User::where('active', true));
  7. Understand the TrendValue data object

    master

    The TrendValue class is a data object that represents a single point in a trend calculation. It encapsulates the specific date and the calculated aggregate value for that period.

    It contains two properties:

    • date: A string representing the date or time period of the trend point.
    • aggregate: A mixed type value representing the result of the aggregation (e.g., a sum, count, or average) for that specific date.