MBS Mohammed Baobaid
All projects
Project
Public release Open Source Software Created 22 December 2022

Laravel Custom Chart

A lightweight Laravel package that turns Eloquent records into chart-ready aggregate time series.

Created 22 December 2022, Laravel Custom Chart is a small package I built to generate chart-ready datasets directly from Eloquent models. It accepts a model, chart title, aggregate function, field, interval count, interval type, total toggle, and date format, then returns a structured array ready for custom chart components.

Laravel PHP Composer Package Eloquent Carbon CarbonPeriod Package Auto-discovery
Narrated walkthrough

This audio is not a word-for-word copy of the case below. You can read the written case while listening to me explain the project in more detail.

0:00 / 0:00
Speed
Laravel Custom Chart project preview
devhereco/custom-chart Package
2.2 Latest version
count/sum/avg Aggregates
day-week-month-year Intervals

Role

Package author and Laravel developer

Outcome

I shipped a reusable chart-data helper with Laravel package auto-discovery, Composer metadata, aggregate support for count, sum, and average, flexible date grouping, period totals, and percentage-change signals.

The Challenge

Dashboard pages often need the same kind of chart payload: records grouped by date, aggregated into counts or totals, and padded so the visual timeline does not break when a day or month has no rows. I wanted that logic to live in one reusable package instead of being rewritten inside every controller.

The Approach

I put the workflow behind CustomChart::create(...). The package queries the selected model, normalizes each record date with Carbon, groups the collection by the chosen interval, applies the requested aggregate function, fills the missing interval keys with zero through CarbonPeriod, then returns totals and percentage-change values alongside the series.

How it works

I wrapped repeated chart logic into one package

The core idea was simple: a Laravel developer should be able to pass an Eloquent model into one helper and receive the data structure needed for a dashboard chart. Instead of mixing grouping, aggregation, date formatting, and comparison math into controllers, the package keeps that work inside a reusable class.

I made the API flexible without making it heavy

CustomChart::create(...) accepts the model class, title, aggregate function, aggregate field, interval count, interval type, total toggle, and date format. That gave the package enough control for common dashboard needs while keeping the call small enough to use directly in a controller or service.

The package groups Eloquent records into chart intervals

Records are sorted by created_at, grouped through Carbon by day, week, month, or year, and then reduced with the selected aggregate. For count charts the package can count rows; for financial or operational charts it can sum or average a chosen numeric field.

Missing intervals are filled deliberately

A chart should not skip a date just because there were no matching records. The package builds the expected timeline with CarbonPeriod and fills absent intervals with zero. That makes the output easier to feed into chart libraries because the x-axis stays stable.

What this project shows

This was an early package-building project, and it shows the way I was learning to separate reusable Laravel behavior from one-off application code. The code is compact, but the intent is practical: turn a repeated dashboard need into a Composer package with clear inputs, predictable output, and Laravel auto-discovery.

Results

  • Packaged chart generation as `devhereco/custom-chart` with PSR-4 autoloading and Laravel provider auto-discovery
  • Supported count, sum, and average aggregation against any Eloquent model field
  • Added interval grouping for days, weeks, months, and years with configurable output date formats
  • Returned zero-filled series so chart components can render consistent timelines even when no records exist for a period
  • Included optional total output plus current/previous month and year comparison values
  • Maintained the package release history through version 2.2 after the original 2022 launch

Key features

01 Single `CustomChart::create(...)` entry point for generating chart datasets from Eloquent models
02 Aggregate support for count, sum, and average values
03 Date filtering by days, weeks, months, and years
04 Zero-filled intervals so chart timelines stay consistent when records are missing
05 Optional total output plus current and previous month/year comparison values
06 Laravel service provider registration through package auto-discovery

Tech stack

Laravel PHP Composer Package Eloquent Carbon CarbonPeriod Package Auto-discovery
Project links

Interested in similar work?

I build systems like this for teams that need reliable engineering, clean interfaces, and measurable outcomes.